Start of topic | Skip to actions
InheritanceWith inheritance we are able to make use of the a-kind-of and is-a relationship. As described there, classes which are a-kind-of another class share properties of the latter. In our point and circle example, we can define a circle which inherits from point:
class Circle inherits from Point {
attributes:
int radius
methods:
setRadius(int newRadius)
getRadius()
}
Class Circle inherits all data elements and methods from point. There is no need to define them twice: We just use already existing and well-known data and method definitions.
On the object level we are now able to use a circle just as we would use a point, because a circle is-a point. For example, we can define a circle object and set its center point coordinates:
Circle acircle acircle.setX(1) /* Inherited from Point */ acircle.setY(2) acircle.setRadius(3) /* Added by Circle */"Is-a" also implies, that we can use a circle everywhere where a point is expected. For example, you can write a function or method, say move(), which should move a point in x direction:
move(Point apoint, int deltax) {
apoint.setX(apoint.getX() + deltax)
}
As a circle inherits from a point, you can use this function with a
circle argument to move its center point and, hence, the whole circle:
Circle acircle
...
move(acircle, 10) /* Move circle by moving */
/* its center point */
Let's try to formalize the term ``inheritance'':
Definition (Inheritance) Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say "A inherits from B". Objects of class A
thus have access to attributes and methods of class B without the need to redefine them. The following definition defines two terms with which we are able to refer to participating classes when they use inheritance.
Definition (Superclass/Subclass) If class A inherits from class B, then B is called superclass of A. A is called subclass of B. Objects of a subclass can be used where objects of the corresponding superclass are expected. This is due to the fact that objects of the subclass share the same behaviour as objects of the superclass.
In the literature you may also find other terms for ``superclass'' and "subclass". Superclasses are also called
parent classes. Subclasses may also be called child classes or just derived classes.
Of course, you can again inherit from a subclass, making this class the superclass of the new subclass. This leads to a hierarchy of
superclass/subclass relationships. If you draw this hierarchy you get an inheritance graph.
A common drawing scheme is to use arrowed lines to indicate the inheritance relationship between two classes or objects.
In our examples we have used "inherits-from". Consequently, the arrowed line starts from the subclass towards the superclass as illustrated in the figure below:
Figure 5.5: A simple inheritance graph.
In the literature you also find illustrations where the arrowed lines
are used just the other way around. The direction in which
the arrowed line is used, depends on how the corresponding author has decided to understand it.
Anyway, within this tutorial, the arrowed line is always directed towards the superclass.
In the following sections an unmarked arrowed line indicates "inherit-from".
%HELPEDITING% Comments | |||||