the super keyword
super is a reference variable that is used to refer immediate parent class object.
Uses of super Keyword:
class Vehicle{
int speed=50;
}
class Bike extends Vehicle{
int speed=100;
void display(){
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
}
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
class Vehicle{ int speed=50; } class Bike extends Vehicle{ int speed=100; void display(){ System.out.println(super.speed);//will print speed of Vehicle now } public static void main(String args[]){ Bike b=new Bike(); b.display(); } }
2.super is used to invoke parent class constructor.
class Vehicle{ Vehicle(){System.out.println("Vehicle is created");} } class Bike extends Vehicle{ Bike(){ super();//will invoke parent class constructor System.out.println("Bike is created"); } public static void main(String args[]){ Bike b=new Bike(); } }
3. super can be used to invoke immediate parent class method.
class Person{ void message(){System.out.println("welcome");} } class Student extends Person{ void message(){System.out.println("welcome to java");} void dislay(){ message();//will invoke current class message() method super.message();//will invoke parent class message() method } public static void main(String args[]){ Student s=new Student(); s.display(); } }
JAVA TUTORIALS HOMEPAGE
Uses 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.
1.super is used to refer immediate parent class instance variable.
Problem without super keyword
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
class Vehicle{ int speed=50; } class Bike extends Vehicle{ int speed=100; void display(){ System.out.println(super.speed);//will print speed of Vehicle now } public static void main(String args[]){ Bike b=new Bike(); b.display(); } }
2.super is used to invoke parent class constructor.
class Vehicle{ Vehicle(){System.out.println("Vehicle is created");} } class Bike extends Vehicle{ Bike(){ super();//will invoke parent class constructor System.out.println("Bike is created"); } public static void main(String args[]){ Bike b=new Bike(); } }
3. super can be used to invoke immediate parent class method.
class Person{ void message(){System.out.println("welcome");} } class Student extends Person{ void message(){System.out.println("welcome to java");} void dislay(){ message();//will invoke current class message() method super.message();//will invoke parent class message() method } public static void main(String args[]){ Student s=new Student(); s.display(); } }
JAVA TUTORIALS HOMEPAGE
0 comments: