Proofread inheritance section
parent
e1d4cecf2b
commit
973a1e4100
|
@ -294,12 +294,13 @@ creating a new class instance.
|
||||||
|
|
||||||
@node Inheritance
|
@node Inheritance
|
||||||
@section Inheritance
|
@section Inheritance
|
||||||
Inheritance is a touchy subject. It can be a powerful feature, but it can also
|
Inheritance can be a touchy subject among Object-Oriented developers. It is a
|
||||||
be easily abused. @dfn{Inheritance} is the term used to describe the process of
|
powerful feature that is easily abused. @dfn{Inheritance} is the term used to
|
||||||
creating a @dfn{child} class that @dfn{extends} (inherits members from) another
|
describe the process of creating a @dfn{child} class that @dfn{extends}
|
||||||
@dfn{parent} class. The parent class is also referred to as the @dfn{supertype}
|
(inherits members from) another @dfn{parent} class. The parent class is also
|
||||||
and the child is called the @dfn{subtype}. Let's consider the following example,
|
referred to as the @dfn{supertype} and the child is called the @dfn{subtype}.
|
||||||
where we have a dog, lazy dog, and a dog that walks on two legs:
|
Let's consider the following example, where we have a dog, a lazy dog, and a dog
|
||||||
|
that walks on two legs:
|
||||||
|
|
||||||
@float Figure, f:inheritance-ex
|
@float Figure, f:inheritance-ex
|
||||||
@image{img/inheritance-ex}
|
@image{img/inheritance-ex}
|
||||||
|
@ -308,7 +309,7 @@ where we have a dog, lazy dog, and a dog that walks on two legs:
|
||||||
|
|
||||||
In the above example, we would say that @var{LazyDog} and @var{TwoLeggedDog}
|
In the above example, we would say that @var{LazyDog} and @var{TwoLeggedDog}
|
||||||
are @emph{subtypes} of @var{Dog}, and that @var{Dog} is the @emph{supertype}
|
are @emph{subtypes} of @var{Dog}, and that @var{Dog} is the @emph{supertype}
|
||||||
of the other two. We describe inheritance as an ``is a'' relationship. That is:
|
of the two. We describe inheritance as an ``is a'' relationship. That is:
|
||||||
|
|
||||||
@itemize
|
@itemize
|
||||||
@item
|
@item
|
||||||
|
@ -323,10 +324,11 @@ of the other two. We describe inheritance as an ``is a'' relationship. That is:
|
||||||
|
|
||||||
Subtypes @dfn{inherit} all public and protected members of their supertypes
|
Subtypes @dfn{inherit} all public and protected members of their supertypes
|
||||||
(@pxref{Member Visibility}). This means that, in the case of our above example,
|
(@pxref{Member Visibility}). This means that, in the case of our above example,
|
||||||
the @code{walk()} method would be available to our subtypes. If the subtype also
|
the @code{walk()} and @code{bark()} methods would be available to our subtypes.
|
||||||
defines a method of the same name, as was done above, it will @dfn{override} the
|
If the subtype also defines a method of the same name, as was done above, it
|
||||||
parent functionality. For now, we will limit our discussion to public members.
|
will @dfn{override} the parent functionality. For now, we will limit our
|
||||||
How would we represent this class using ease.js?
|
discussion to public members. How would we represent these classes using
|
||||||
|
ease.js?
|
||||||
|
|
||||||
@float Figure, f:inheritance
|
@float Figure, f:inheritance
|
||||||
@verbatim
|
@verbatim
|
||||||
|
@ -366,7 +368,7 @@ How would we represent this class using ease.js?
|
||||||
@end float
|
@end float
|
||||||
|
|
||||||
You should already understand how to define a class (@pxref{Declaring Classes}).
|
You should already understand how to define a class (@pxref{Declaring Classes}).
|
||||||
The above example introduced two means of @dfn{extending} classes, of defining a
|
The above example introduced two means of @dfn{extending} classes -- defining a
|
||||||
new class that inherits from a parent:
|
new class that inherits from a parent:
|
||||||
|
|
||||||
@table @strong
|
@table @strong
|
||||||
|
@ -401,7 +403,7 @@ conciseness.
|
||||||
@subsection Understanding Member Inheritance
|
@subsection Understanding Member Inheritance
|
||||||
In @ref{f:inheritance}, we took a look at how to inherit from a parent class.
|
In @ref{f:inheritance}, we took a look at how to inherit from a parent class.
|
||||||
What does it mean when we ``inherit'' from a parent? What are we inheriting? The
|
What does it mean when we ``inherit'' from a parent? What are we inheriting? The
|
||||||
API.
|
answer is: the API.
|
||||||
|
|
||||||
There are two types of APIs that subtypes can inherit from their parents:
|
There are two types of APIs that subtypes can inherit from their parents:
|
||||||
|
|
||||||
|
@ -463,19 +465,19 @@ even though we did not define it ourselves.
|
||||||
@subsection Overriding Methods
|
@subsection Overriding Methods
|
||||||
When a method is inherited, you have the option of either keeping the parent's
|
When a method is inherited, you have the option of either keeping the parent's
|
||||||
implementation or overriding it to provide your own. When you override a method,
|
implementation or overriding it to provide your own. When you override a method,
|
||||||
you replace whatever functionality was defined by the parent. Using this concept
|
you replace whatever functionality was defined by the parent. This concept was
|
||||||
is how we made our lazy dog lazy and our two-legged dog walk on two legs in
|
used to make our @var{LazyDog} lazy and our @var{TwoLeggedDog} walk on two legs
|
||||||
@ref{f:inheritance}.
|
in @ref{f:inheritance}.
|
||||||
|
|
||||||
After overriding a method, you may still want to invoke the parent's method.
|
After overriding a method, you may still want to invoke the parent's method.
|
||||||
This allows you to augment the functionality rather than replacing it entirely.
|
This allows you to @emph{augment} the functionality rather than replacing it
|
||||||
ease.js provides a magic @code{__super()} method to do this. This method is
|
entirely. ease.js provides a magic @code{__super()} method to do this. This
|
||||||
defined only for overriding methods and calls the parent method that was
|
method is defined only for the overriding methods and calls the parent method
|
||||||
overridden.
|
that was overridden.
|
||||||
|
|
||||||
In order to demonstrate this, let's add an additional subtype to our hierarchy:
|
In order to demonstrate this, let's add an additional subtype to our hierarchy.
|
||||||
@var{AngryDog}. @var{AngryDog} will be a subtype of @var{LazyDog}. Not only is
|
@var{AngryDog} will be a subtype of @var{LazyDog}. Not only is this dog lazy,
|
||||||
this dog lazy, but he's rather moody.
|
but he's rather moody.
|
||||||
|
|
||||||
@float Figure, f:super-method
|
@float Figure, f:super-method
|
||||||
@verbatim
|
@verbatim
|
||||||
|
|
Loading…
Reference in New Issue