Inheritance and its forms
Need Of Inheritance
Inheritance is a splendid concept of object oriented-languages. There are several reasons why inheritance was introduces into object oriented language. Here are some major reasons behind the introduction of inheritance.
- One major reason behind this is capability to express the inheritance relationship which ensures the closeness with the real-world models.
- Another reason is the idea of reusability . The advantages of reusability are : faster development time, easier maintenance, and easy to extend. Inheritance allows the addition of additional features to an existing class without modifying it. One can derive a new class (subclass or derived class) from an existing one and add new features to it. For example, Suppose we have a class Student, and we need to add a new class called GraduateStudent . We derive the new class GraduateStudent from the existing class Student and then all we need to add are the extra features to GraduateStudent that describe the differences between students and graduate students. Notice the reduction in amount of typing efforts.
- One reason is transitive nature of inheritance. For example, if a new class GraduateStudent has been declared as a subclass of Student which itself is a subclass of Person then GraduateStudent must also be a Person i.e, inheritance is transitive. If a class B inherits properties of another class A . This property is called transitive nature of inheritance.
When a new class is derived from an existing class then new derived class is called derived class or subclass and the existing class is called base class or super class . And it is said that the subclass (or derived class) inherits from the base class. Inheritance may take place in many forms which are being discussed below:
- Single Inheritance. When a subclass inherits only from one base class, it is known as single inheritance.
- Multiple Inheritance. When a subclass inherits from multiple base classes, it is known as multiple inheritance.
- Hierarchical Inheritance. When many sub classes inherit from a single base class, it is known as hierarchical inheritance.
- Multilevel Inheritance. The transitive nature of inheritance is reflected by this form of inheritance. When a subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance.
- Hybrid Inheritance. Hybrid inheritance combines two or more forms of inheritance for example, when a sub class inherits form multiple base classes and all of its base classes inherit from a single base class, this form of inheritance is known as hybrid inheritance.
![]() |
Types Of Inheritance |
Comments
Post a Comment