Type of Inheritance:
Based on Number of ways Inheriting the features of base class into derived class, it have Five Types, they are:
Single Inheritance:
In Single Inheritance, there exists single base class and single derived class. We already discussed the example of single inheritance in Our Inheritance tutorial: This type of inheritance enables a derived class to inherit properties and behavior from a single parent class or base class. thus enabling code reuse-ability as well as adding new features to the existing code. This makes the code much more elegant and less repetitive.
The Basic Syntax for Single Inheritance is:
Access-Specifier class Base_Class
{
// Base Class Body:
}
class Derived_Class : Base_class
{
// Derived Class Body:
}
Multiple Inheritance:
Multiple Inheritance represent a kind of inheritance when a derived class inherits properties of Multiple classes. For Example, there are three classes A, B, and C and derived Class is D as shown below.
The Basic Syntax for Multiple Inheritance is:
Access-Specifier class A
{
// Base Class Body:
}
Access-Specifier class B
{
// Base Class Body:
}
class C : A , B
{
// Derived Class Body:
}
Multi-Level Inheritance:
Multiple Inheritance represents a type of inheritance when a Derived class is a base class for another class. In Other words, deriving a class from a derived class is known as multi-level Inheritance. Simple multi-level Inheritance is shown in below image where Class Grandfather is a parent of Class Father and Class Father is a parent of Class Child.
In Above Picture, Father Inherited the Properties of Grandfather, and Child Inherited the Properties of Father. The Basic Syntax for Multi-Level Inheritance is:
Access-Specifier class A
{
// Base Class Body:
}
class B : A
{
// Derived Class Body:
}
class C : B
{
// Derived Class Body:
}
Hierarchical Inheritance:
When there is a Need to create Multiple Derived Classes That Inherit Properties of the same Base Class is known as Hierarchical Inheritance. For Example: Civil, Computer, Mechanical, Electrical are derived from Engineer. Natural language, Programming Language are derived from language.
The Basic Syntax for such type of Inheritance is:
Access-Specifier class Base_Class
{
// Base Class Body:
class A : Base_class
{
// Derived Class Body:
}
class B : Base_class
{
// Derived Class Body:
}
class C : Base_class
{
// Derived Class Body:
}
Hybrid Inheritance:
Combination of Multi-level and Hierarchical Inheritance will give us Hybrid Inheritance. this type of inheritance is also known as Virtual Inheritance.