Inheritance is the process of using properties of one class into the another class

  • Inheritance allows to use properties of the existing class into the another class by deriving sub-class from the base class.
  • A class that is inherited is called a super class,base class or parent class and the derived class is called a sub-class, derived class or child class.
  • Inheritance allows the creation of hierarchical classifications.
  • It provides re-usability of the existing class without re-creating from the scratch
  • A sub-class is a specialized version of a super class. It inherits all of the instance variables and methods defined by the super class and add its own, unique elements.
  • We can inherit a class by using the extend keyword with the class definition.
  • The general form of class declaration that inherits a super class is as follow:

Syntax:

class subclass-name extends super class-name
{
    // body of class.
}
  • We can only specify one super class for any subclass.
  • Java does not support the inheritance of multiple super-classes into a single subclass.
  • We can create a hierarchy of inheritance in which a subclass becomes a super class of another subclass. However, no class can be a super class of itself.
  • A subclass inherits all the members of its super class but it cannot access private members of the super class.
class A
{
    int i, j;
    private int x;
    voidshowij()
    {
        System.out.println ("i and j: - "+ i + " " + j);
    }
}

class B extends A
{
   int k;
   voidshowk()
   {
      System.out.println("k="+k);
   }
   void sum()
   {

      System.out.println("i+j+k="+(i+j+k));
   }

}

class SimpleInheritance
{
    public static void main(String args[])
   {
       A superob= new A();
       B subob= new B();
       superob.i=10;
       superob.j=20;
       
       System.out.println("Content of superob:");
       superob.showij();
       System.out.println();
       
       subob.i=7;
       subob.j=8;
       subob.k=9;
       //subob.x=10    can not access private member
       System.out.println("Content of subob:");
       subob.showij();
       subob.showk();
       
       System.out.println();
       System.out.println("sum of i,j and k in subob=");
       subob.sum();
   }

}

Output:

Contents of superob:
I and j= 10 20
Content of subob:
I and j= 7 8
K=9
Sum of I, j and k in sumob =  I + j + k = 24

Use of super

  • Whenever a subclass needs to refer to its immediate super class, it can do so by using the keyword super.
  • Super has 2 general forms
  1. To call super class’ constructor.
  2. To access members of the super class that has been hidden by member of sub class
Super to call super class Constructor
  • A subclass can call a constructor defined by its super class using the following form of super.

super(parameter-list)

  • Where parameter-list specifies parameter required by the constructor in the super class.
  • super() must be the first statement inside a subclass constructor.
  • To see how super() is used, consider the following constructor of BoxWeight() class:
BoxWeight(double h,double w, double d,double wt)
{
    Super(h,w,d);
    Weight=wt;
}
  • When a subclass calls super(), it is calling the constructor of its immediate super class. Thus, super() always refer to the super class immediately above the calling class. This is also true in a multi-leveled hierarchy.

Second use of super:

  • The super can also be used to refer to the member and method of super class. This usage has the following general form:
super.member
  • Member can be either a method or an instance variable.
  • This second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the super class. Consider this simple class hierarchy.
class A
{
   int i;
}
class B extends A
{
   int i;  //this I hides the I in A
   B(int a, int b)
   {
      super.i=a;
      i=b;
   }
   void show()
   {
      System.out.println("I in super class:" + super.i);
      System.out.println("I in subclass:"+i);
   }
}

class UseSuper
{
    public static void main(String args[])
    {
        B subob=new B(1,2);
        subob.show();
    }
}
  • Although, the variable I in B hides the I in A, super allows access to the I defined in the superclass. Super can also be used to call methods that are hidden by a subclass.