Pages

Monday 30 September 2013

Inheritance basics, super keyword and constructor calling


Inheritance Basics: 

                 It is the process by which one object inherits the properties of other object. In my previous post I have explained the basic OOP’s principles where I have explained the general meaning of inheritance. In Java, one class inherits the other class. The class which inherits the other is called subclass and the class which is inherited by other class is called superclass. 

                General syntax used to execute inheritance is shown below: 

                     class SubclassName extends SuperClass

                Consider the below example program where a subclass Y and a superclass X are implemented. The extend keyword is used to implement inheritance in Java. In below example program class Y inherits the class X i.e. class Y extends class X.

package blog.inheritance;

public class X
{
  int a;
  private int b; // variable b is used to show that private variables can't be accessed by subclass.
 

 
  void addMethod(int a,int b)
  {  
              System.out.println();
              System.out.println("Am addMethod in class X");
              System.out.println("addition result "+(a+b));
  }
}

package blog.inheritance;

public class Y extends X
{
   int x=20;
   int y=20;
  
   {
     a=x;
     //y=b; // b is not visible here, because b is private variable of class X.
    
   }
   
   void CallAfromBMethod()
   {
               System.out.println("am Class Y, see i can access all variable of Class X, bcs i inherit class X");
               System.out.println("but sad!! i cant access private variables of class X");
               addMethod(a,y);
   }
  
   public static void main(String[] args)
   {
               Y objectY=new Y();
              
               objectY.CallAfromBMethod();
   }
  
}

Output:

am Class Y, see i can access all variable of Class X, bcs i inherit class X
but sad!! i cant access private variables of class X

Am addMethod in class X
addition result 40


                  In the above example program you can see class Y inherits the class X. Here class X is superclass and class Y is subclass. The subclass Y can access all the class members of its superclass X except the private class members. In example you can see variable ‘b’ is private member of class X and when subclass Y tries to access this variable it results in error!! In program you can see the code fragment “y=b” is commented out in class Y.

Note: A subclass can access all the class members of its superclass directly, except private class members. But a superclass cannot access any class members of its subclasses directly.

Important facts of Inheritance:

1.     A subclass can access all the class members of its superclass directly, except the private members. 
2.     A superclass cannot access any class member of its subclass directly.
3.     A subclass can be a superclass of another subclass.
      
     
 
                               

4.     A superclass can have more than one subclass. 
5.     A subclass can have only one superclass. More than one superclass for a subclass is not permitted in java.

                 

                 
6.     A superclass variable can be used as reference to a subclass object. For example see example program below. 

 The Super Keyword:
                 There are two forms in which super keyword in java is used. 
a)     To call the superclass constructor.
b)    To access the class member of superclass. This is just like this keyword used in java. But super refers to the superclass object.

  To call the superclass constructor: 
 
                The general form of super keyword used to call superclass constructor is shown below: 
                            super (parameter-list);
              In which condition it will be necessary to use super keyword to call the superclass constructor?      
       
                   There will be situation in which superclass will have private instance variable, in such case it is not possible for subclass to initialize this variables directly. In order to help programmers like us in such situations, Java provides us with this super (parameter-list) keyword to call the superclass constructor.
Note to not note down: Java is awesome right? It has answers for everything.
The below program example will help you to understand the usage of super keyword to call the superclass constructor.
package blog.inheritance.Super1;

public class X
{
   private int a;
   private int b;
  
   X(int a,int b)
   {
               this.a=a;
               this.b=b;
   }
  
   void addition(int z)
   {
               int add=a+b+z;
               System.out.println();
               System.out.println("am method in X,addition result is "+add);
   }
}

package blog.inheritance.Super1;

public class Y extends X
{
           
   int c;
  
   Y(int a,int b,int c)
   {
     super(a, b);// super used to call superclass constructor to initialize private variable of superclass X
               this.c=c;
   }
  
   void additonINXYVar()
   {
              System.out.println("am method in Y class,i vl call method in my superclass X for addition");
              addition(c);
   }

  
   public static void main(String[] args) // main method
   {
              Y objY=new Y(10, 20, 30);
              objY.additonINXYVar();
   }
}

Output:

am method in Y class, i vl call method in my superclass X for addition

am method in X, addition result is 60


Note: This ‘super (parameter-list)’ must be the first statement to be executed in subclass constructor. 
                  In the above example program you can see that subclass Y of class X uses the super keyword to call its’ superclass X’s constructor to initialize its’ private variables.

To access the class member of superclass: 
                  The general form of super keyword used to access the class members of superclass is shown below. 
                               super.classMember
 
Note: Class members are instance variables and methods of a class.
               This form of super is used in to access those superclass members which are hidden by the subclass members with same name.


package blog.inheritance.Super1;

public class A
{
   int a=10;
   int b=20;
  
   void addMethod(int x,int y,int z)
   {
               int sum=x+y+z;
               System.out.println();
               System.out.println("am method in Class A ");
               System.out.println("sum is "+sum);
   }
  
}

package blog.inheritance.Super1;

public class B extends A
{
  int a; // same name variable as in the superclass
  int b; //same name variable as in the superclass
  int c;
 
  void callMethod()
  {
              a=super.a;// value of superclass variable is assigned to subclass variable with the same name.
              b=super.b;// value of superclass variable is assigned to subclass variable with the same name.
              c=40;
              System.out.println("am method in class B");
              System.out.println("i will call method in my superclass A");
              addMethod(a, b, c);// direct call to superclass method
  }
 
  public static void main(String[] args)
  {
             B objB=new B();
             
             objB.callMethod();
  }
 
}


Output:

am method in class B
i will call method in my superclass A

am method in Class A
sum is 70


                  In the above program you can see how super keyword is used to access the data members of superclass which are hidden in subclass by the data members in subclass with the same name. 

Order of Constructor calling in Inheritance environment:

               The constructors are called in the order from superclass to subclass. The below diagram represent the order in which constructors of superclass and its subclasses are invoked. 





                                                 
In the diagram above you can see, 
           Constructor of class X is invoked first.
           Constructor of class Y is invoked second.
           Constructor of class Z is invoked third.

                The below is given to illustrate how the call to superclass and its subclass constructors are executed. 
package blog.inheritance.Super1;

public class X
{
  
   X()
   {
             System.out.println("am constructor form class X"); 
   }
  
  
}

package blog.inheritance.Super1;

public class Y extends X
{
           
  
   Y()
   {
              System.out.println("am construtor from class Y");
   }
  
  
}

package blog.inheritance.Super1;

public class Z extends Y
{
   Z()
   {
               System.out.println("am constructor from class Z");
   }
  
   public static void main(String[] args)
   {
             Z z1=new Z();
   }
}

Output:
am constructor form class X
am construtor from class Y
am constructor from class Z

                From the output of above program example you can see that how the constructors are called from superclass to its subclasses.