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:

class derivedClass extends baseClass
{
// body of the derivedClass.
}

An example derived class syntax for Java:

class Dog extends Animal
{
// body of the Dog Class.
}

Derived class syntax for C++:

class derivedClass : visibilityMode baseClass
{
// body of the derivedClass.
}

An example derived class syntax for C++:

class Dog : public Animal
{
// body of the Dog Class.
}

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

Inheritance Types

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:

---------- For Animal ----------
This is an Animal!
Moving... - Animal
Speaking... - Animal
Eating... - Animal
Type: Animal
Name: Animal
------------ For Dog ------------
This is an Animal!
This is a Dog!
The Dog is running.
Woof! Woof! Woof!
Eating... - Animal
Type: Animal
Name: Spike
The Dog is deleting...
Animal is deleting...
Animal is deleting...

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

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:

---------- For Animal ----------
This is an Animal!
Moving... - Animal
Speaking... - Animal
Eating... - Animal
Type: Animal
Name: Animal
------------ For Dog ------------
This is an Animal!
This is a Dog!
The Dog is running.
Woof! Woof! Woof!
Eating... - Animal
Type: Animal
Name: Spike
------------ For Baby Dog ------------
This is an Animal!
This is a Dog!
This is a Baby Dog!
The Dog is running.
Weef! Weef! Weef!
Eating... - Animal
Type: Animal
Name: Tyke
The Baby Dog is deleting...
The Dog is deleting...
Animal is deleting...
The Dog is deleting...
Animal is deleting...
Animal is deleting...

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

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:

---------- For Animal ----------
This is an Animal!
Moving... - Animal
Speaking... - Animal
Eating... - Animal
Type: Animal
Name: Animal
------------ For Dog ------------
This is an Animal!
This is a Dog!
The Dog is running.
Woof! Woof! Woof!
Eating... - Animal
Type: Animal
Name: Spike
----------- For Bird -----------
This is an Animal!
This is a Bird!
The Bird is flying.
Chirp! Chirp!
Eating... - Animal
Type: Animal
Name: Tweety
----------- For Fish -----------
This is an Animal!
This is a Fish!
The Fish is swimming.
Speaking... - Animal
Eating... - Animal
Type: Animal
Name: Nemo
The Fish is deleting...
Animal is deleting...
The Bird is deleting...
Animal is deleting...
The Dog is deleting...
Animal is deleting...
Animal is deleting...

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

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++:

class derivedClass
: visibilityMode BaseClass1, visibilityMode BaseClass2
{
// body of the derivedClass.
}

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.

a.C::function(); 
a.B::function();

Output of the code:

This is constructor for B class.
This is constructor for C class.
This is constructor for A class.
Function of C class
Function of B class
This is destructor for A class.
This is destructor for C class.
This is destructor for B class.

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

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.

class B : virtual public D,
......
class C : virtual public D
......

Avoiding multiple inheritance as much as possible is necessary.

Output of the code:

This is constructor for D class.
This is constructor for B class.
This is constructor for C class.
This is constructor for A class.
Variable: 123456This is destructor for A class.
This is destructor for C class.
This is destructor for B class.
This is destructor for D class.

The Startup

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

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 & +792K 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 & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

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