For example, if you
want to access a method named sayHello, you should write parent::sayHello();
Please note that we didn't write any function named sendEmail() in HtmlEmailer
class, but that method is working from its parent, Emailer class.
In the above example, HtmlEmailer is a subclass of Emailer class and
Emailer class is a superclass of HtmlEmailer. You must remember that
if the subclass has no constructor in it, the constructor from superclass
will be invoked. At the time of writing this book, there is no support for
multiple inheritances at class level. This means you can't extend more
than one class at a time. However multiple inheritance is supported in
interfaces. An interface can extend an arbitrary number of other interfaces
at a time.
Kick-Starting OOP
[ 30 ]
Overriding Methods
In an extended object you can override any method (either declared as protected or
public) and perform anything as you wish. So how can you override any method?
Simply create a function with the same name that you want to override. For example,
if you create a function name sendEmail in HtmlEmailer class, it will override the
sendEmail() method of its parent, Emailer class.
Pages:
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51