What is the Inheritance in

What is the Inheritance in Object-oriented programming?

You may have heard the term Inheritance in any object-oriented programming language like Java, Python, C++, PHP, etc.

And here we will understand this core concept of OOP. But before going further, I have assumed that you already know what is OOP and what is Class & Objects.

What is Inheritance in OOP?

Let’s understand Inheritance through an example –

Suppose you have two classes ClassA and ClassB, and inside the ClassB, you want to use some or all properties and methods of the ClassA, and you can do this with the help of Inheritance.

So basically, It is used to get the methods and properties of other classes in a class.

In the following image, you can see that Class B inherits the Class A.

In some programming languages, the extends keyword is used to inherit a class. But not in all languages, you may find that there are different ways to implement the Inheritance.

Inheritance Example
The Class B inherits the Class A.

What are the Parent and Child classes in OOP?

The Parent and Child are two terms to identify the classes when you use inheritance.

Let’s look at an example to understand the Parent and Child classes –

Suppose you have two classes ClassA and ClassB, and you have inherited the methods and properties of the ClassA into the ClassB. In this case ClassA is a Parent class and ClassB is a Child Class.

  • Parent class is also known as Super Class, and Base Class.
  • Child Class is also known as Sub Class, and Derived Class.

Types of Inheritance

1. Single inheritance

Single inheritance is where a single class inherits characteristics from another single class.

Single Inheritance Example
Example of Single Inheritance.

2. Multilevel inheritance

When more than two classes are inherited as a chain, it is called multilevel inheritance. In this type of inheritance, a child class may have its own child classes.

In the following image you can see the Class C can access the Methods and Properties of Class B as well as Class A. Because, the Class C found the characteristics of the Class A inside the Class B, because Class B inherits the Class A.

Multilevel inheritance
Multilevel inheritance

3. Hierarchical inheritance

When a parent class has multiple child classes is called Hierarchical inheritance.

Hierarchical inheritance
Hierarchical inheritance.

4. Multiple inheritances

If a Child class inherits more than one parent class is called Multiple inheritance.

Note: Some languages do not support multiple inheritance, such as Java, PHP, etc.

Multiple Inheritance
Multiple Inheritance.

5. Hybrid inheritance

A combination of two or more types of inheritance is called Hybrid Inheritance.

Hybrid Inheritance
Hybrid Inheritance.