INHERITANCE

Inheritance


Getting super class properties into sub class is called as Inheritance(OR) inheritance is a process of acquiring the properties of parent class into child class.
  • In java we can achieve Inheritance concept by using “extends”/”implements” keyword.
  • Java supports multilevel inheritance but doesn’t support multiple inheritance.
  • Constructors can’t be inherited into subclass. Only properties and behaviors means attributes and methods will be inherited.
  • In java every class must & should become a subclass to any other class.
  • Incase if it doesn’t extends any other class,then compiler will extends with Object class for that class.

Syntax for Inheritance :

 
                        
                    class A          /* parent class/superclass (JAVA) */ 
                    {
                     
                    }
                    class B   extends A   /* child class/subclass (C#) */
                    {            
                  
                    }     
                        

Types of INHERITANCES (Reusable techniques):

Based on inheriting the properties from base class to the derived class, in JAVA we have five types of inheritances. They are as follows :

(i)  Single Inheritance :

    Single inheritance is one in which there exists single base class and single derived class.

javapadho
Single Level Inheritance

(ii)  Multi Level Inheritance :

    Multilevel inheritance is one in which there exist single base class, single derived class and n number of intermediate base classes.

javapadho
Multi Level inheritance

    An intermediate base class is one, which acts as base class in one context and in another context it acts as derived class.


(iii)  Hierarchical Inheritance :

    Hierarchical inheritance is one in which there exists single base class and n number of derived classes.

javapadho
Hierarchical inheritance

(iv)  Multiple Inheritances :

    Multiple inheritance is one in which there exists n number of base classes and a single derived class.

javapadho
Multiple Inheritance

    Multiple inheritances are not supported by JAVA through classes,But it is supported by JAVA through the concept of interfaces.


(v)  Hybrid Inheritance :

    Hybrid inheritance = combination of any available inheritances types.

javapadho
Hybrid Inheritance

    In the above combinations,2nd combination of Hybrid inheritance is not supported in JAVA through the concept of classes, but it is supported through the concept of interfaces.Why because it is a combination of Hierarchical + Multiple Inheritance.Since java classes doesn’t support the concept of Multiple inheritance.

Multilevel Inheritance :

  • Getting sub class properties by using another subclass is called multilevel inheritance.

Example 1:

 
						
                    class Parent
                    {
                        int i;
                    }
                    class Son extends Parent
                    {
                        int j;
                    }
                    class Gdson extends  Son
                    {
                        void m1()
                        {
                           System.out.println(i);
                           System.out.println(j);
                        }
                    }
                    class Mngr
                    {
                        public static void main(String[] args)
                        {
                            Parent ram=new Parent();
                            Son ram1=new Son();
                            Gdson gs=new Gdson();
                            gs.m1();
                        }
                    } 
                        

   Output:

0 0

Explanation :

  • In the above example Parent is a super class and Son is subclass to Parent class and Gdson is subclass to Son class.
  • Son class get the properties from parent class.
  • Gdson class also get the properties from both Son class And Parent class

Multiple Inheritance :

  • Deriving one class by using more than one super class.
  • But in java a class can’t support multiple inheritance.
  • If we want to achieve the multiple inheritance it can be done through the interfaces only. In interfaces there is no constructor.

Example 2:

 
						
                     class Parnt1
                     {
                        int i;
                     }
                     class parnt2
                     {
                        int j;
                     }
                     class Son extends Parnt1,Parnt2
                     {
                        /* Here compilation fails  because  of class Son  has ambiguity error , */
                        /* means  class Son does not know which class constructor has to be executed */
                        /* for object creation i.e . either Parnt1()  or Parent2(). */
                        void m1()
                        {
                           System.out.println(i);
                           System.out.println(i);
                        }
                     }
                     public class Mngrs
                     {
                        public static void main(String[] args)
                        {
                           Parnt1 p=new Parnt1();
                        }
                     }                        

Explanation :

  • Java class can’t support multiple inheritance because of constructor chaining.
  • The compiler will be confused for calling either first class constructor or second class constructor.
  • so the compiler is in ambiguity stage

Example 3:

 
						
                     package com.sdj.pack1;
                     class Ab
                     {
                        int i;
                        void test()
                        {
                           System.out.println("test-a");
                        }
                     }
                     class Bc extends Ab
                     {
                        int j;
                        void m1()
                        {
                           System.out.println("m1");
                        }
                     }
                     class Inherit 
                     {
                        public static void main(String[] args)
                        {
                           Ab a1=new Ab();
                           Bc b1=new Bc();
                           b1.m1();
                           b1.test();
                           a1.test();
                        }
                     }                        
                       	

   Output:

m1 test-a

Example 4:

 
						
                     class A
                     {
                        int i=19;
                        A(int i)
                        { 
                           System.out.println(i); 
                        }
                     }
                     class B extends A
                     {
                        int i=9;
                        B()
                        {
                           super(20);
                        }
                     }
                     public class Inhrt
                     {
                        public static void main(String[] args)
                        {
                           /* A a1=new A(); */ 
                           /* Here compilation  fails because  there are  no No Args constructor */
                           /* inside the class A. To make success  we should  pass integer type */ 
                           /* constructor only like A a1=new A(10).*/
                           B b1=new B();
                           A a2=new B();  /* sub class */ 
                           /* B b2=new A(); */
                           /* Here compilation fails  because  we can’t assign  the sub class */
                           /* reference variable to super class object .*/
                           System.out.println(a1.i);
                           System.out.println(b1.i);
                           System.out.println(a2.i);   
                           /* System.out.println(b2.i); */
                           /* Here compilation  fails  because of  using  super  class reference */
                           /* variable and with this we can able to access inherited properties */ 
                           /* from super class to sub class. But we can’t access the members  */
                           /* which are  specific to sub class */
                        }
                     }                        
                       	

   Output:

10 20 20; 19 9 19

Explanation :

  • B b1=new B();//Here compilation is success because It is a calling statement for no-arg constructor in class B.
  • A a2=new B();//Here compilation is success because we can assign the super class reference variable into sub class object so that it can run.

Example 5:

 
						
                     class A
                     {
                        int i=0;
                        A()
                        {
                           super();   
                        }
                     }
                     class B extends A
                     {
                        int j=19;
                        B()
                        {
                           super();     
                        }
                     }
                     public class Manager
                     {
                        public static void main(String[] args) 
                        {
                           A a1=new A();
                           B b1=new B();
                           A a2=new B();
                           /* B b2=new A(); */
                           /* Here compilation  fails  because  we can’t assign  the */
                           /* sub class reference variable to super class object . */
                           System.out.println(a1.i);
                           System.out.println(b1.j);
                        }
                     }                        
                       	

   Output:

0 19
Scroll to Top Button