Concepts of OOPs-3 Inheritance
A child inherits the property of his parent. He can acquire new properties or modify the inherited one. Similarly, inheritance is a feature of OOP, which allows making use of the existing class without making changes to it. This can be achieved by deriving a new class (Derived class) from the existing class (Base class). A derived class inherits all the properties of the base class. More properties can be added to the derived class, if needed. Therefore, the complexity of the derived class may grow as the level of inheritance grows. There is no limit to the level of inheritance.
For example, scooter is a class in itself. It is also a member of two wheelers class. Two wheelers class in turn is a member of an automotive class as shown below:
Main advantage of inheritance is reusability. Reusability means that we can add additional features of an existing class without modifying it. It is shown below:
It is important to note in above figure , features A and B, which are part of the base class, are common to both the derived classes, but each derived class also has features of its own.
Let’s see what are the needs and objectives of inheritance are:
It is required for extending the functionality of an existing class.
It can be used to establish “a kind of” relationship.
The redundancy among classes can be reduced.
It helps in reuse of an existing class by derived class.
No comments:
Post a Comment