Class inheritance
Sometimes an object is just a special case of a more general object
or it is slightly different from some other object.
In this case, it would be pointless to duplicate the class code of such an object.
One technique that can be used to avoid this is the so-called class inheritance.
In the inheritance relation a class can be connected to the other class using
For example:
is a definition of two classes
(for more about classes see Classes).
The essence of the inheritance relation is, that object of the above class
For example, in the code:
there is created a new object of class
Notice, that the second object created by inheritance relation is assigned to the first object,
but it is created in lazy way, which means that it will not be created at the time of creation of the first object,
but will be created the first time when it is needed (perhaps never).
When it is not specified which object to inherit from
(no
Inheritance chain
The first object inherits from the second object.
The second object can also inherits from the third object, and so on.
In this way we will get an inheritance chain of objects, linked by inheritance relation.
There are special indicator objects, that can be accessed inside the code of given class,
to handle objects of inheritance chain directly, these are:
Direct indication
The another way of linking objects in inheritance chain is a direct indication of
the object after which will inherit from, using keyword
For example:
it defines class
Chains and cycles
Because the direct indication can make inheritance chains of miscellaneous objects,
then some objects may exists in more then one chain and also inheritance chain may contains cycles.
Notice also, that in chains which are linked by direct indication, pointer
Initialization of the under object
Since objects in inheritance chain are created in lazy way, it is possible to pass parameters to the constructor
of next object before it will be created.
There is special syntax to do that: writing vector of parameters after keyword
Example:
The result of that statement is