Inheritance Types in OOP

What is Inheritance?

dilusha sandaruwani
3 min readJun 1, 2024

Inheritance is a very powerful feature of OOPs. It is a mechanism of acquiring properties or behaviors of existing class to a new class. In other words, A class inherits data fields or methods from another class.

For eg. Animal is a class and other classes like Dog, cat etc. can inherit the common properties of animal class.

Inheritance provides following benefits:

  • Less development and maintenance costs
  • Code reusability
  • Reduces code redundancy and supports code extensibility
  • Save time and effort to write codes

Types of inheritance

In Object-Oriented Programming (OOP), there are several types of inheritance. These types differ based on the number of superclasses and subclasses involved. Below are the main types of inheritance:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

1. Single Inheritance:

In this type, a subclass inherits from only one superclass. For example, in Java, if class X inherits from class A, it is written as:

class X extends A

Languages that support this type of inheritance include Java, C++, and Python.

2. Multiple Inheritance:

In this type, a subclass can inherit from more than one superclass. This type of inheritance is not supported in Java but can be seen in languages like C++ and Python. For example, if class C inherits from both class A and class B, it is written as:

class C extends A, B

3. Multilevel Inheritance:

In this type, inheritance occurs in a chain. One class inherits from another class, which in turn inherits from another class, and so on. For example, if class C inherits from class B and class B inherits from class A, it is written as:

class C extends B
class B extends A

This can be seen in languages like Java, C++, and Python.

4. Hierarchical Inheritance:

In this type, multiple subclasses inherit from a single superclass. For example, if class B and class C inherit from class A, it is written as:

class C extends B
class B extends A

This type can be seen in languages like Java, C++, and Python.

5. Hybrid Inheritance:

This type is a combination of two or more types of inheritance. For example, a combination of Hierarchical and Multiple Inheritance can form a Hybrid Inheritance. This type can be observed in languages like Python.

Happy Learning and Fun Coding!!!😊.

Sources:

--

--

dilusha sandaruwani

Software Engineering undergraduate at University of Kelaniya