Wednesday, November 15, 2023

Types of Inheritance

A derived class can inherit some or all of the features of the base class. A class can inherit features from more than one class and one or more than one level.

Based on these factors, the inheritance can be classified into different types. They are

1. Single Inheritance

2. Multiple Inheritance

3. Multi Level Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance

6. Multi-path inheritance

Single Inheritance:-

If a derived class is derived from only one base class, then it is called “single inheritance‟.



Syntax:

 class A

 {

  public:

  …….

  };

 class B : public B

 {

 public:

 …….

};

Multiple Inheritance:-

If a derived class is derived from more than one base class is called “Multiple Inheritance‟.



Syntax:

class A

{

public:

---------

};

class B

{

public:

---------

};

class C : public A, public B

{

public:

---------

};

Multi Level Inheritance:-

If a derived class is derived from another derived class that has been already derived from one base class, is called “Multi Level Inheritance‟.



Syntax:

class A

{

public:

---------

};

class B : public A

{

public:

---------

};

class C : public B

{

 public:

---------

};

Hierarchical Inheritance:-

If number of derived classes are derived from a single base class is called” Hierarchical Inheritance‟.



Syntax:

class A

{

public:

---------

};

class B : public A

{

 public:

---------

};

class C : public A

{

public:

---------

};

class D : public A

{

public:

---------

};

Hybrid Inheritance:-

Deriving a class that involves more than one form of inheritance is called hybrid inheritance.


In the above example, it has both multi-level and hierarchical inheritance. So, it shows hybrid inheritance.

Multi-path Inheritance:-

If a derived class is derived from other derived classes which have been already derived from the same base class is called multi-path inheritance. It forms grandparent, parent and child relationship.

In the above Fig. the derived class D has two immediate base classes – B and C. Both these base classes are themselves derived from the base class A.

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top