Types of Inheritance
A class can inherit properties from one or more classes and from one or more levels.
On the basis of this concept, there are five types of inheritance.
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Single Inheritance
In single inheritance, a class is derived from only one base class. The example and figure below show this inheritance.Example
class A
{
members of A
};
class B : public A
{
members of B
};
Multiple Inheritance
In this inheritance, a class is derived from more than one base class. The example and figure below show this inheritance.Implementation Skeleton:
class A
{
members of A
}; class B
{ members of B
};
class C :
public A,
public B
{
members of C
};
Hierarchical Inheritance
In this type, two or more classes inherit the properties of one base class. The example and figure below show this inheritance.Implementation Skeleton:
class A
{
members of A
};
class B
{
members of B
};
class C :
public A,
public B
{
members of C
};
Multilevel Inheritance
The mechanism of deriving a class from another derived class is known as multilevel inheritance. The process can be extended to an arbitrary number of levels. The example and figure below show this inheritance.Implementation Skeleton:
class A
{
members of A
};
class B :
public A
{
members of B
};
class C :
public B
{
members of C
};
Hybrid Inheritance
This type of inheritance includes more than one type of inheritance mentioned previously. The example and figure below show this inheritance.Example
class A
{
members of A
};
class B :
public A
{
members of B
};
class C :
public A
{
members of C
};
class D :
public B, public C
{
members of D
};
Post A Comment:
0 comments so far,add yours