Life is continued journey for one big reason that living beings are able to create new creatures of their own types by passing many attributes of them. This is called Inheritance.
Since Object Oriented Languages boast of modelling the real life and real problems, so they need to implement this feature called inheritance. Infacts it goes much beyond the real life as in this case child classes can be inherited from multiple parent classes also.
To implement Inheritance we need a mechanism of creating derived classes. The class from which other class is derived is called the parent class, super class or base class and the class to which it is derived to is called the child class, sub class or derived class.
Let us consider some real life examples which are self explanatory in terms of who is parent and who is child. We have used the term base class, sub class and sub-sub class for three level hierarchy, but believe me it is always a relative term and quite obviously sub class can also be called a base class of sub-sub class. This hierarchy is expandable both on the top side and on the bottom side.
The first two examples are easy to comprehend with diagrams given here.
Here you can see in the automobile example that every class has only one parent. Also we can notice that if car is a four wheeler than it is an automobile two. Which means that there is a property transmission from grand parent also. This is called the transitive nature of class hierarchy.
In the Animal and Flying Thing example you can see that class Bird has two parent classes Animal and Flying Thing. Similarly Bat has two parent classes Mammal and Flying Thing. The transitive nature may still be maintained. A Bat is a Mammal and an Animal too.
Some Mathematical examples are here.
The above examples have clear hierarchy with easy to understand transitive nature.
Let us see some business examples from the business world also.
The above examples are also fairly simple and straight forward.
Converting the interest examples as computer code will be as follows Let us examine the code for Interest class and its subclasses.
In the above example we have created two derived classes Simple and Compound from its base class Interest. When we are using the objects of derived classes si and ci, we can use the functions within the child class and with in the parent class as well. This is in a way that a child is able to use resources of his parent also. In this example we can see that from object si and ci we are able to call amount function also which is a function of parent or base class.
However we can further explore that use of parents resources does come with some accessibility and visibility restrictions given by public, protected and private visibility and accessibility status. This will be discussed in subsequent topics on this site.