Inheritance


Inheritance is one of the core features of OOP (Object-oriented programming). It is a capability which allows a class to inherit the properties (data members and member functions) of another class. It involves two or more classes.


Sub Class (or, Derived Class): It is the class which inherits the properties from an other class.

Super Class (or, Base Class): It is the class whose properties get inherited by sub class.


Advantages of Inheritance:


i. Inheritance provide us the feature of code reusability i.e. once a code is written, it can be further reused again and again.

ii. It reduces the chances of error and data redundancy.

iii. Avoids duplication of data.


Visibility Modes of Inheritance 


It is very important to know that which members of the base class can be derived by sub classes. This is based upon the visibility modes:

1. Public Mode: Using this, the public and protected member of the base class remains public and protected members in the sub class.

2. Protected Mode: Using this, the public and protected members of the base class become protected members in the sub class.

3. Private Mode:  Using this, the public and protected members of the base class become private members in the sub class.


Hence, the private members of the super class are not accessible by the sub classes.


Syntax:

class superclass_name

{

  //body

};

class subclass_name : access_mod superclass_name      //access_mod denotes the visibilty mode

{

  //body

};

int main()

{

  subclass_name object_name:

  //body

}


Example: 

//C++ program to demonstrate inheritance

#include<iostream.h>

#include<string.h>

using namespace std;

class super      //super class

{

  public:

  string var="x";

};

class sub:public super        //sub class using public visibility mode

{

  public:

  int  val= 17;

};

int main()        //main function

{

   sub obj;

   cout<<"Variable is "<<obj.var;

   cout<<"\nAnd its value is: "<<obj.val;

   return 0;

}


OUTPUT:





In the above example, 'obj' object of 'sub' class has all data members and member functions of 'super' class. Here, the public data member of class 'super' gets derived by the sub class 'sub' because a derived class is publicly inherited from a base class.


Types of Inheritance 


1. Single inheritance: The inheritance which involves exactly one sub class and a super class is called a single or simple inheritance. i.e. a sub class  is derived from a super class.

Syntax:

class subclass_name : access_mod superclass_name

{

  //body

};


Example: 

//C++ to demonstrate single inheritance

#include<iostream.h>

#include<conio.h>

using namespace std;

class inheritance    //super class

{

  public:

  display()

  {

     cout<<"Inheritance is a feature of OOP.";

  }

};

class single : public inheritance      //class single deriving class inheritance 

{

};

int main()

{

  single obj;      //object obj has access to all properties of superclass inheritance 

  obj.display();

  cout<<"\nThis is an example of single inheritance.";

  return 0;

}

OUTPUT:




2. Multilevel inheritance: It involves two or more super classes in such a way that a sub class is derived from another sub class.


Example: 

//C++ program to demonstrate multilevel inheritance

#include<iostream.h>

#include<conio.h>

using namespace std;

class super        //super class

{

  public:

  display()

  {

     cout<<"Inheritance is a feature of OOP.";

  }

};

class sub1: public super       //sub1 derived from super class

{

};

class sub2 : public sub1        //sub2 class is derived from sub1

{

};

int main()

{

   sub2 obj;

   obj.display();

   cout<<"\nThis is an example of multilevel inheritance.";

   return 0;

}


OUTPUT:





3. Multiple inheritance: In this, a sub class is derived from two or more super classes. Hence, it involves more than one super class.


Syntax:

class subclass_name: access_mod1 super_class1, access_mod2 super_class2, ... 

{

  //body

};


Example: 

//C++ program to demonstrate multiple inheritance

#include<iostream.h>

#include<conio.h>

using namespace std;

class super1            //1st super class

{

   public:

   display()

   {

      cout<<"C++ is an object oriented programming language.";

   }

};

class super2             //2nd super class

{

   public:

   show()

   {

      cout<<"\nInheritance is a feature of OOP.";

   }

};

class sub : public super1, public super2          //sub class derived from two super classes

{

};

int main()

{

  sub obj;

  obj.display();

  obj.show();

  cout<<"\nThis is an example of multiple inheritance.";

  return 0;

}

OUTPUT:




4. Hierarchical Inheritance: It involves two or more sub classes in such a way that the sub classes are derived from a single super class.


Syntax:

class sub_class1: access_mod super_class

{

  //body 

};

class sub_class2: access_mod super_class

{

  //body

};


Example: 

//C++ program to demonstrate hierarchical inheritance

#include<iostream.h>

#include<conio.h>

using namespace std;

class super            //super class

{

  public:

  display()

  {

     cout<<"Inheritance is a feature of OOP.";

  }

  show()

  {

     cout<<"\nThis is an example of hierarchical inheritance.";

  }

};

class sub1 : public super           //1st sub class

{

};

class sub2 : public super            //2nd sub class

{

};

int main()

{

  sub1 obj1;         

  sub2 obj2;

  obj1.display();        //calling display() of super class with object created by 1st sub class


  obj2.show();             //calling show() of super class with object created by 2nd sub class

  return 0;

}

OUTPUT:




 5. Hybrid Inheritance:  It involves two or more types of inheritances.


Example: 

//C++ program to demonstrate hybrid inheritance

#include<iostream.h>

#include<conio.h>

using namespace std;

class super1

{

  public:

  display()

  {

     cout<<"C++ is an example of object oriented programming language."<<endl;

  }

};

class super2

{

   public:

   show()

  {

      cout<<"Inheritance is a feature of OOP."<<endl;

  }

};

class sub1: public super2

{

};

class sub2: public super1, public sub1

{

};

int main()

{

  sub2 obj;

  obj.display();

  obj.show();

  cout<<"\nThis is an example of hybrid inheritance.";

  return 0;

}

OUTPUT:



Note: In the next tutorial, you will learn about special cases of hybrid inheritance.


Note: If you don't know what OOP(C++) is, we suggest that you read our C++ tutorial.