Super Keyword in Java
Super Keyword in Java: is used to refer parent class object. It is used inside a sub-class method definition to call a method defined in the super class.
Problem without super:
class Vehicle
{
int speed=50;
}
class Bike3 extends Vehicle
{
int speed=100;
void display(){
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[])
{
Bike3 b=new Bike3();
b.display();
}
}
Output:
100
Solution with super:
Example of super keyword
class Vehicle
{
int speed=50;
}
class Bike4 extends Vehicle
{
int speed=100;
void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}
public static void main(String args[])
{
Bike4 b=new Bike4();
b.display();
}
}
Output:
50
No comments :
Post a Comment