What is Inheritence ?

What is inheritence ?

 

  • The Mechanism of deriving of new class from an old one is called inheritence .
  • The Old Class referred as a base class and the new class referred as a derived class or subclass.

 

Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

or example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.

This concept of Inheritance leads to the concept of polymorphism.

Features or Advantages of Inheritance:

Reusability:

Saves Time and Effort:

Increases Program Structure which results in greater reliability.

Inheritance Example:

class exforsys
{
public:
exforsys(void) { x=0; }
void f(int n1)
{
x= n1*5;
}void output(void) { cout<<x; }private:
int x;
};

class sample: public exforsys
{
public:
sample(void) { s1=0; }

void f1(int n1)
{
s1=n1*10;
}

void output(void)
{
exforsys::output();
cout << s1;
}

private:
int s1;
};

int main(void)
{
sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}


Types Of Inheritence :

  1. 1.     Single inheritence  :
  • Ø A derived clas with only one base class is called single inheritence.

 

  1. 2.     Multiple inheritence :
  • Derived class with several base classes.

 

  1. 3.     Hierachical inheritence :
  • Traits of one class may be inherited by more than one class.

 

  1. 4.     Multilevel inheritence :
  •  The mechanism of deriving a class from another derived class .

 

  1. 5.     Hybrid inheritences :

                 There could be a situation where we need to apply two or more type of inheritence.

Leave a comment