follow

Saturday, 6 July 2013

super keyword in java : JAVA Programming by "Gopal Krishna"

The super keyword in java is a reference variable that is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.

Usage of java super Keyword

  1. super is used to refer immediate parent class instance variable.
  2. super() is used to invoke immediate parent class constructor.
  3. super is used to invoke immediate parent class method.

1) super is used to refer immediate parent class instance variable.

Problem without super keyword
 class Vehicle
    { 
      int speed=50; 
    } 
   
    class Bike3 extends Vehicle
    { 
      int speed=100; 
      void display()
        { 
           System.out.println(speed);//will print speed of Bike  
        } 
         
    }     
    class stest     
    {
      public static void main(String args[])
    { 
       Bike3 b=new Bike3(); 
       b.display(); 
    } 
    }

Output:100

In the above example Vehicle and Bike both class have a common property speed. Instance variable of current class is refered by instance bydefault, but I have to refer parent class instance variable that is why we use super keyword to distinguish between parent class instance variable and current class instance variable.
Solution by super keyword
  1. //example of super keyword  
  2.   
  3. class Vehicle{  
  4.   int speed=50;  
  5. }  
  6.   
  7. class Bike4 extends Vehicle{  
  8.   int speed=100;  
  9.       
  10.   void display(){  
  11.    System.out.println(super.speed);//will print speed of Vehicle now  
  12.   }  
  13.   public static void main(String args[]){  
  14.    Bike4 b=new Bike4();  
  15.    b.display();  
  16.      
  17. }  
  18. }  
Output:50

2) super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent class constructor as given below:
  1. class Vehicle{  
  2.   Vehicle(){System.out.println("Vehicle is created");}  
  3. }  
  4.   
  5. class Bike5 extends Vehicle{  
  6.   Bike5(){  
  7.    super();//will invoke parent class constructor  
  8.    System.out.println("Bike is created");  
  9.   }  
  10.   public static void main(String args[]){  
  11.    Bike5 b=new Bike5();  
  12.         
  13. }  
  14. }  
Output:Vehicle is created
       Bike is created



Note: super() is added in each class constructor automatically by compiler.





3) super can be used to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used in case subclass contains the same method as parent class as in the example given below:
  1. class Person{  
  2. void message(){System.out.println("welcome");}  
  3. }  
  4.   
  5. class Student16 extends Person{  
  6. void message(){System.out.println("welcome to java");}  
  7.   
  8. void display(){  
  9. message();//will invoke current class message() method  
  10. super.message();//will invoke parent class message() method  
  11. }  
  12.   
  13. public static void main(String args[]){  
  14. Student16 s=new Student16();  
  15. s.display();  
  16. }  
  17. }  



class xyz
{
xyz()
{
System.out.println("I am default");
}
xyz(int i)
{
System.out.println("The value ->"+i);
}
}

class xyz1 extends xyz
{
xyz1(int i)
{
super(i);
System.out.println("The value of i ->"+i);
}
}

class test
{
public static void main(String arg[])
{
xyz1 x1=new xyz1(10);
}
}

No comments:

Post a Comment