What Is Inheritance?
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
- Multilevel Inheritance
- Hierarchical Inheritance
In C++, five inheritance types supports:
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Note: Multiple inheritance is not supported in Java through class.
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.
}
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.
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 inherits 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: SpikeThe Dog is deleting...
Animal is deleting...
Animal is deleting...
!! 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.
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: TykeThe 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 !!
3- Hierarchical Inheritance
When several classes are derived from same base class it is called 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: NemoThe 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 !!
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.
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 classThis 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 !!
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.
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.