Wednesday, 19 November 2014

What is super keyword ?

Super Keyword: 

The super 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 super Keyword super is used to refer immediate parent class instance variable. super() is used to invoke immediate parent class constructor. super is used to invoke immediate parent class method.

Usage of 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 using super keyword:

class Car{
int price=5000;
}
class Honda extends Car{
int price=6000;
void show(){
System.out.println(price);//will print speed of Honda
}
public static void main(String []args){
Honda b=new Honda();
b.show();
  }
}

Output: 6000

In the above example Car and Honda both class have a common property price.Instance variable of current class is refered by instance by default,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.

Solution by using super keyword:

class Car{
int price=5000;
}
class Honda extends Car{
int price=6000;
void show(){
System.out.println(super.price);//will print speed of price now
}
public static void main(String [] args){
Honda b=new Honda();
b.show();
  }
}

Output: 5000

>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:

Example of super keyword:

class Car{
Car() { System.out.println("Car is created");}
}
class Honda extends Car{
Honda(){
super();    //will invoke parent class constructor
System.out.println("Honda is created");
}
public static void main(String [] args){
Honda b=new Honda();
  }
}

Output: Car is created
              Honda is created


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


Note:As we know well that default constructor is provided by compiler automatically but it also adds super() for the first statement of constructor.If you are creating your own constructor and you don't have either this() or super() as the first statement,compiler will provide super() as the first statement of the consructor.

Another example of super keyword where super() is provided  by the compiler implicitely(automatically).

class Car{
Car() { System.out.println("Car is created");}
}
class Honda extends Car{
int speed;
Honda(int price){
this.price=price;
System.out.println(price);
}
public static void main(String []args){
Honda b=new Honda(1000);
  }
}

Output: Car is created


>3.super can be used to invoke immediate 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:

class College{
void greeting(){System.out.println("welcome in College");}
}

class Student extends College{
void greeting() { System.out.println("welcome to B.Tech");}
void show(){
greeting();    //will invoke current class greeting() method
super.greeting();   //will invoke parent class greeting() method
 }
public static void main(String []args){
Student s=new Student();
s.show();
 }
}

Output: welcome to B.Tech
               welcome in College

In the above example Student and College both classes have greeting() method if we call greeting() method from Student class, it will call the greeting() method of Student class not of College class because priority is given to local.

In case there is no method in subclass as parent, there is no need to use super. In the example given below greeting() method is invoked from Student class but Student class does not have greeting() method, so you can directly call greeting() method.

class College{
void greeting(){ System.out.println("welcome in College");}
}
class Student extends College{
void show(){
greeting();  //will invoke parent class greeting() method
}
public static void main(String []args){
Student s=new Student();
s.show();
  }
}

Output: welcome in College




1 comment :