Friday 28 April 2017

Inheritance OOP


Inheritance OOP Banner

Inheritance (Object Oriented Programming)


In object-oriented programming,Inheritance allows us to define a class which is based on another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.The derived class is also called subclass and the base class is also known as super-class. The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class.inheritance saves our money and time both because we use predefined compile and tested class which increase quality of software.


Single Inheritance

In single inheritance a child class have only one parent,base or super class . single inheritance is most useful type from all available  inheritance.


Inheritance Basic Graph

Example
  • Animal is derived from living things
  • Car is derived from vehicle
  • Typist is derived from staff
class base
{
      public:
      void basefun()
      {
            cout<<endl<<"Base Class Function";
      }
};
class derive : public base
{
      public :
      void derivefun()
      {
            cout<<endl<<"Derive class function";
      }
};
void main()
{
      derive ob;
      ob.basefun();
      ob.derivefun();

}

 Hierarchical Inheritance 


Hierarchical inheritance is a type of inheritance in which a single parent or base class has more one child or derives class. Hierarchical inheritance is useful when if there is a need of flow some common functionality in whole system for example in database application a base class have connection string which use by the entire child.

 Hierarchical Inheritance

class base
{
      public:
      void basefun()
      {
            cout<<endl<<"Base Class Function";
      }
};
class child1 : public base
{
      public :
      void child1_fun()
      {
            cout<<endl<<"child1 class function";
      }
};
class child2 : public base
{
      public :
      void child2_fun()
      {
            cout<<endl<<"child2 class function";
      }
};

void main()
{
      child1 ob1;
      child2 ob2;
      ob1.basefun();
      ob1.child1_fun();
      ob2.basefun();
      ob2.child2_fun();
}

Multi-Level Inheritance

In multiple inheritances a child class is inherited further by other class. In diagram you can see multi-level inheritance construct structure like ladder. Class A is inherited by Class B so attributes of class A will be transfer in class be directly and B is further inherited by Class C. So class C has both class attributes class B as well as class A. Attributes of class A transfer into class C though class B. You can see inheritance effect in figure by color class C have three color blue and yellow is coming form base classes.

Multi-Level Inheritance

class base
{
      public:
      void basefun()
      {
            cout<<endl<<"Base Class Function";
      }
};
class Level1 : public base
{
      public :
      void Level1_fun()
      {
            cout<<endl<<"Level1 class function";
      }
};
class Level2 : public Level1
{
      public :
      void Level2_fun()
      {
            cout<<endl<<"Level2 class function";
      }
};

void main()
{
      Level2 ob;
      ob.basefun();
      ob.Level1_fun();
      ob.Level2_fun();
}


Multiple Inheritance 


When a child class has more than one parent or base class then this type of inheritance is known as multiple inheritances. Multiple inheritances are not support by most of the modern object oriented languages. Because it has drawback that a derive class may have some common attribute more than one time which create ambiguity error in C++ compiler.


class base1
{
      public:
      void base1_fun()
      {
            cout<<endl<<"Base1 Class Function";
      }
};
class  base2
{
      public :
      void base2_fun()
      {
            cout<<endl<<"Base2 Class Function";
      }
};
class child : public base1, public base2
{
      public :
      void child_fun()
      {
            cout<<endl<<"Child class function";
      }
};

void main()
{
      child ob;
      ob.base1_fun();
      ob.base2_fun();
      ob.child_fun();
}


Problem with multiple inheritance



Multiple inheritance some times as in following figure shows if such type of construction is there then it will create a problem. For example if class a has a method named show then this method will be transfer to class B and C respectively and class B and C are further inherit by class D . So class D will inherit two version of show function one from B and other one from C. This will create ambiguity problem which was resolve in C++ by using virtual keyword. That is why from my opinion multiple inheritance is not support by most of object oriented language.




No comments:

Post a Comment