Different types of Inheritance in OOPs with Example

Purpose of this session is to share importance of Inheritance. We will discuss about C++ inheritance in short. Also we will discuss the importance different types of inheritance with Example.

What is Inheritance?

Process of inheritance is, “Object of one class can acquire the properties of another class.”. Lets take an example I have a base class “birds”. It has some properties & methods. If I want to distinguish between flying & non-flying birds then these classes can be derived from birds class (As the base class). Because common methods & properties for flying birds & non-flying birds must be there is birds class.

Fig: Example of Inheritance

Example of Inheritance

Inheritance supported by high level languages like c++, java, c sharp, javascript or python. Here to present you a real-time example I am sharing inheritance in js. In the below Codes I have a person class with 4 properties. I am inheriting Teacher class from the base class person. Teacher class has 5 properties. 4 properties from person class and 1 additional property salary.

// Constructor Class
function person(firstname, lastname, age, gender) {
this.name = { firstname, lastname };
this.age = age;
this.gender = gender;
// Prototyping Person Class
person.prototype.greeting = function() {
alert('Hi! I\'m ' + this.name.firstname + '.');
var personInstance = new person("Bikash", "Dash", 27, "M", 20000);
// Teacher Class inherits from Person Class
function Teacher(firstname, lastname, age, gender, salary) {
person.call(this, firstname, lastname, age, gender);
this.salary = salary;
// To assign Person prototype we have to Create Prototype for Teacher
Teacher.prototype = Object.create(person.prototype);
var newTeacher = new Teacher("Baby", "Roy", 26, "F", 30000);
// After Teacher.Prototype only this will work

Here if we alert(Teacher.prototype.constructor); it will show Person Class Constructor. To display Teacher Class Constructor we have to execute the following line of Code.

Teacher.prototype.constructor = Teacher;

Types of Inheritance

There are 5 types of Inheritance available in Object Oriented Programming (OPPs), Those are as below:

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

1. Single Inheritance

Single inheritance is very much easy to understand. One class that is derived class extends another class or base class this mechanism is Single inheritance.

Fig: Single Inheritance

Refer to the above example think you have a base class Office. While Creating an Employee Class for Office related properties you can get in Office class. Here two things comes into picture base class & derived class. This scenario is called Single Inheritance.

2. Multilevel Inheritance

One derived class extends from another derived class this mechanism is known as Multilevel Inheritance.

Fig: Multilevel Inheritance

Sometime it happens a derived class brings property from parent of parent class. Lets talk about your Grand Father here your parents are the derived class for your grand father & you are the derived class for your parents. But you have some properties of your Grand Father.

3. Multiple Inheritance

In Multiple Inheritance, there are several base classes with single derived class. The issue with this inheritance is that more than one base class will have to manage by single derived class.

Fig: Multiple Inheritance

In the software projects, Multiple Inheritance is very rarely used. C++ supported the Multiple Inheritance.

4. Hierarchical Inheritance

In Hierarchical Inheritance, one class is inherited by more than one class.

Fig: Hierarchical Inheritance

5. Hybrid Inheritance

Combination of Single Inheritance and Multiple Inheritance are called as Hybrid Inheritance. More than one type of inheritance is used in the different design.

Fig: Hybrid Inheritance

Advantages of Inheritance

  • One of the most important advantage of inheritance is “Re-usability”. Don’t need to write similar classes for several instances.
  • Data hiding mechanism. Functionality Oriented rather rotate around Codes.
  • Inheritance makes application code more flexible to change or update.
  • Overriding – We can able to override the base class methods.