What Is Inheritance?

Muzaffer Arda Uslu
Dec 14, 2020 · 6 min read
taken from https://realpython.com/

Inheritance is an Object Oriented Programming (OOP) feature that allows the properties of an object to be used by different objects.

  • A written class can be inherited by another class.
  • When this process is done, all properties of the base class are transferred to the new class.
  • Reusability of the written code is ensured.
  • A base class is the parent class of a derived class.
  • The parent class of derived class is called Base Class, and the inherited class is called the Derived Class.

In Java, three inheritance types supports:

  • Single Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance

In C++, five inheritance types supports:

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

Multiple inheritance is not supported in Java.

Derived class syntax for Java:

An example derived class syntax for Java:

Derived class syntax for C++:

An example derived class syntax for C++:

Accessibility of Variables

Visibility Mode in C++

I’m gonna show code examples in C++ programming language.

Inheritance Types

1- Single Inheritance

Single inheritance enables a derived class to inherit properties and behavior from a single parent class.

UML for Single Inheritance

Let’s see Single Inheritance Code

When we look at the UML table, it is seen that the Dog class does not have the eat() method, but the Dog class has this method because it inherit from the Animal class. When this method is called in Main function, the eat() method of the inherited Animal class is called.

When we look at the table again, the Dog class has move() and speak() methods. Although these methods are in the inherited class, they have been rewritten using ‘OVERRIDE’. For this reason, when these methods are called in main, the related class’s move() or speak() class is called.

Output of the code:

!! Also pay attention the order of calling Constructors and Destructors !!

2- Multilevel Inheritance

Multilevel inheritance refers to a mechanism in OOP where a class can inherit from a derived class, thus making that derived class the base class for the new class.

UML for Multilevel Inheritance

Let’s see Multilevel Inheritance Code

Output of the code:

!! Also pay attention the order of calling Constructors and Destructors !!

3- Hierarchical Inheritance

When several classes are derived from same base class it is called hierarchical inheritance.

UML for Hierarchical Inheritance

Let’s see Hierarchical Inheritance Code

When we look at the UML table, it is seen that the Dog and Bird classes do not have the eat() method, while the Fish class does not have eat() and speak() methods. All three classes have these methods because they inherit from the Animal class.

When these methods are called in Main, the eat() and speak() methods of the inherited Animal class are called.

When we look at the table again, the Dog and Bird have move() and speak() methods, and the Fish class has the move() method. Although these methods are in the inherited class, they have been rewritten using ‘OVERRIDE’. For this reason, when these methods are called in main, the related class’s move() or speak() class is called.

Output of the code:

!! Also pay attention the order of calling Constructors and Destructors !!

4- Multiple Inheritance

When an object or a class can inherit characteristics and features from more than one parent class, it is called Multiple inheritance.

UML for Multiple Inheritance

Derived class syntax for C++:

Let’s see Multiple Inheritance Code

When we look the main function, we created an A Class object and we call function() method that is inherited from Class C and Class B. The compiler can not know which inherited function() method have to call. For this reason, we specified that which method of class will be called.

Output of the code:

!! Also pay attention the order of calling Constructors and Destructors !!

5- Hybrid Inheritance

Hybric inheritance is a combination of multiple inheritance and multilevel inheritance. A class is derived from two classes as in multiple inheritance.

UML for Hybrid Inheritance

Let’s see Hybrid Inheritance Code

When we look the A Class’s function() method, we use variable that is inherited class B or class C. Compiler do not know that which class’s variable have to inherit. For this reason, we use virtual keyword. We say to the compiler that only inherit one member thanks to virtual keyword.

Avoiding multiple inheritance as much as possible is necessary.

Output of the code:

The Startup

Get smarter at building your thing. Join The Startup’s +786K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Muzaffer Arda Uslu

Written by

Eskişehir Osmangazi University Computer Engineering Student. https://www.linkedin.com/in/muzafferardauslu/ https://github.com/ardasdasdas

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store