Inheritance Basics


Inheritance is one of the foundations of object-oriented programming because it allows you to create hierarchical classifications. Using hierarchical, you can create a general class that defines common properties for a set of related items. This class can be inherited by other, more specific classes, each adding unique things to it. In Java terminology, a class inherited is called a superclass. The class that is inherited is called subclass. Therefore, a subclass is a special version of a superclass. It inherits all the members defined by the superclass and adds its own, unique element.

Example of inheritance

To inherit a class, you simply incorporate the definition of one class into another by using the "extends" keyword. To see how to let's begin with a short example. The following program creates a superclass called "X" and a subclass class "Y". Notice how the keyword "extends" is used to create a subclass of "X".

Program
//A simple example of Inheritance

//Create a superclass
class X
{
 int _a,_b;
 void SumOfab()
 {
  int sum=this._a+this._b;
  System.out.println("Sum of _a + _b : "+sum); 
 }
}

//Create a subclass by extending class X
class Y extends X
{
 int _c,_d;
 void SumOfcd()
 {
  int sum=this._c+this._d;
  System.out.println("Sum of _c + _d : "+sum); 
 }
 void SumOfabcd()
 {
  int sum=_a+_b+_c+_d;
  System.out.println("Sum of _a + _b + _c + _d : "+sum);
 }
}

public class Main 
{
  public static void main(String[] args) 
  {
    X superobj=new X();
    Y subobj=new Y();
    
    //The superclass may be used by itself.
    superobj._a=10;
    superobj._b=20;
    superobj.SumOfab();
    
    //The subclass has access to all public members of its superclass.
    subobj._a=10;
    subobj._b=20;
    subobj._c=30;
    subobj._d=40;
    subobj.SumOfcd();
    subobj.SumOfabcd();
  }
}

As you can see, the subclass "Y" includes all of the members of its superclass, "X". This is why "subobj" can access "_a" and "_b". Also, inside "SumOfabcd()", "_a" and "_b" can be referred to directly, as if they were part of "Y".

Even though "X" is a superclass for "Y", it is also a completely independent, stand-alone class. Being superclass for subclass does not mean that you cannot use the superclass yourself. A subclass can be a superclass for other subclasses.


A subclass can be a superclass for other subclasses. The following example shows how this is possible.

Program
//A simple example of Inheritance

//Create a superclass
class X
{
 int _a,_b;
 void SumOfab()
 {
  int sum=this._a+this._b;
  System.out.println("Sum of _a + _b : "+sum); 
 }
}

//Create a subclass by extending class X
class Y extends X
{
 int _c,_d;
 void SumOfcd()
 {
  int sum=this._c+this._d;
  System.out.println("Sum of _c + _d : "+sum); 
 }
 void SumOfabcd()
 {
  int sum=_a+_b+_c+_d;
  System.out.println("Sum of _a + _b + _c + _d : "+sum);
 }
}
//Create a subclass by extending class Y
class Z extends Y
{
}

public class Main 
{
  public static void main(String[] args) 
  {
    Z obj=new Z();
    
    //The subclass Z has access to all public members of its superclass X & Y.
    obj._a=10;
    obj._b=10;
    obj.SumOfab();
    obj._c=20;
    obj._d=20;
    obj.SumOfcd();
  }
}