Contact us

Get In Touch

Please contact us, if you have a plan or design in mind that you'd like to share or work with us.

Follow Us On:

User Ajay
795

What is Inheritance?

One of the most powerful characteristics of the Object Oriented approach is code reuse. Inheritance is a process of deriving new classes from existing classes. The existing class is called the base class or super-class and the inherited class is called the derived class or sub-class. The derived classes contain all the attributes and behaviors of the existing classes, plus their own attributes and behaviors.

The parent-child relationship between classes can be represented in a hierarchical view, often called a class tree view that starts with the super-class, while derived classes become more specialized further down the tree.

Inheritance avoids redundancy in code and enables easy maintenance. Any change made to the base class automatically changes the behavior of its sub-classes. In this case, the sub-classes automatically inherit the new behavior.

Types of Inheritance:

  • Single Inheritance
  • Multiple Inheritance
  • Multi-level Inheritance

Now, we'll see these types in detail:

1. Single Inheritance

Single Inheritance is a common form of inheritance. In this case, a child class has only one parent class.

The following figure depicts the concept of single inheritance:

Single Inheritance

In the preceding figure, there will be only one child class (Derived Class) that inherits from the parent class (Base class).



2. Multiple Inheritance

Some object-oriented programming languages allow a child class to inherit from more than one parent class. This is called multiple inheritances.

The following figure depicts the concept of multiple inheritances:

Multiple Inheritance

In the preceding figure, there will be one child class (Derived Class) that inherits from the two parent class (Base class).



3. Multilevel Inheritance

Multi-level inheritance is the enhancement of the concepts of inheritance. When a sub-class is derived from a derived class, the mechanism is known as multi-level inheritance. The derived class is called a sub-class or child class for its parent class, this parent class works as the child class for the class from which it is derived. Multi-level inheritance can go up to any number of levels.

The following figure depicts the concept of multilevel inheritances:

Multilevel Inheritance

In the preceding figure, there will be one child class (Derived Class) that inherits from the only one parent class (Base class) and so on.



Shares  

Comments: