Ad Code

Responsive Advertisement

Inheritance And Types of Inheritance with Real Time Example

                                   
 INHERITANCE
Inheritance and types of inheritance


Ø Process of deriving properties or method from one class to another class.

 Ã˜ Implements IS-A relationship.

 Ã˜ Achieves Code Reusability.

 Ã˜ Super class (Parent class) - the class from which the properties are derived.

 Ã˜ Sub class (Child class) - the class where the properties are derived to.

Ø Super class is also called Base class.

Ø Sub class is also called Derived class.

REAL TIME EXAMPLE:

       FATHER-SON relationship.

v  Superclass - Father.

v  Subclass - Son.

       Method or Properties - Father's asset, Father's characteristics.

Types of Inheritance:

Ø Single Inheritance

Ø Multiple Inheritance

Ø Multilevel Inheritance

Ø Hierarchical Inheritance

Ø Hybrid Inheritance


SINGLE INHERITANCE:

 Ã¼As the name indicates, it contains Single parent class and single child class.

 Ã¼When the properties or behaviour derived from single base class to single child class is called Single Inheritance.

 

Single Inheritance
REAL TIME EXAMPLE:

Single Inheritance
Here,
          Ã¼Animal is the Base Class
          Ã¼Dogs is the Sub Class

MULTIPLE INHERITANCE:

 Ã¼Contains Multiple parent class and single child class.

 Ã¼When the properties and behaviour are derived to single child class from more than one parent class is called Multiple Inheritance.

Multiple Inheritance

REAL TIME EXAMPLE:

Here,
           Ã¼Both the Classes Animals and Mammals are Base Classes.
           Ã¼Dogs is the Sub Class.

MULTILEVEL INHERITANCE:

 Ã¼When the properties are derived to child class from another Derived class is called Multilevel Inheritance.

 Ã¼Here the child class implicitly inherit the properties of base class along with the properties of base class’s parent class(base class).

Multilevel inheritance


Here Class A is the base class of Class B and also implicit base class of Class C.

Class B is the base class of Class C and also Sub class of Class A.

Class C is the child class of Class B.

Class C inherits the properties of  both Class A and Class B.

REAL TIME EXAMPLE:

Multilevel Inheritance Real Time Example
Here,

          üAnimals is the Base class for both Mammals and Dogs Classes.

          üMammal is the sub class of Animal but Base Class of Dogs.

         Ã¼Dogs is the Sub Class and derives properties of both Mammals and Animals Base classes.


HIERARCHICAL INHERITANCE:

üContains Single Parent class (Base Class) and Multiple Child Class (Sub Class).

 Ã¼When more than one sub classes derive properties or behaviour from the same base class is called Hierarchical Inheritance.

Hierarchical Inheritance
REAL TIME EXAMPLE:
Hierarchical Inheritance

Here,

       Ã¼Animal is the Base Class.

      üDogs and Elephants are the Sub Classes of Animal.


HYBRID INHERITANCE:

üCombination of more than one inheritance.


hybrid inheritance


ØClass A to Class D-Single Inheritance.

ØClass A to Class B and Class B to Class D-Multilevel Inheritance.

ØClass A to Class B and Class A to Class C-Hierarchical Inheritance.

ØClass B and Class C to Class D- Multiple Inheritance.

REAL TIME EXAMPLE:

Hybrid Inheritance

Here,

üClass Animals to Class Elephants-Single Inheritance.

üClass Animals to Class Mammals and Class Mammals to Class Elephants-Multilevel Inheritance.

üClass Animals to Class Mammals and Class Animals to Class Herbivores-Hierarchical Inheritance.

üClass Mammals and Class Herbivores to Class Elephants- Multiple Inheritance.









Post a Comment

0 Comments

Close Menu