Monday 6 February 2017

Types of Inheritance


                                                 
types of inheritance


 There are basically 4 types of Inheritance in Java. These are :

   - Single Inheritance
   - Multilevel Inheritance
   - Hierarchical Inheritance
   - Hybrid Inheritance

Java does not  support Multiple inheritance. In java , multiple inheritance is applied via interfaces.

Single Inheritance

This is a simple format , in which A class is inherited by a single base class and so on.

Ex-              class A
              {
                 //Body of class
              }
                  class B Extends A
               {
                  //Body of class
               }

Multilevel Inheritance

In Multilevel inheritance , a single base class is inherited by 2 or more derived classes.

Ex-                 class A
                  {
                        //Body of class A
                  }
                       class B extends A
                   {
                        //Body of class B
                   }
                       class C extends A
                    {
                        //Body of class C
                    }

Hierarchical Inheritance

In this , the class inherits other classes such that it forms a hierarchy , like a tree structure.

Ex-                        class A
                      {
                          //Body of A
                      }
                             class B extends A
                     {
                          //Body of B
                     }
                            class C extends A
                        {
                             //Body of C
                         } 
                            class D  extends B
                     {
                          //Body of D
                     }
                     .
                     .
                     .
                    and so on.


Hybrid Inheritance

Hybrid as the name suggests is the combination of 2 or more inheritance types put together. 

Ex-                          class A
                        {
                            //Body of A
                        }
                               class B extends A                //Single Inheritance
                        {
                             //Body of B
                         }
                                class C extends B
                        {
                                 //Body of C
                        }
                               class D  extends B         // Multilevel Inheritance
                        {
                              //Body of D
                        }

                  
                                                                           By: Knowledge Bits

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *