The super keyword in java is a reference variable that is used to refer immediate parent class object.
/*program with super keyword*/
class Bike
{
int speed=50;
}
class SuperTest extends Bike
{
int speed=100;
void display()
{
System.out.println(super.speed);//will print speed of Bike
}
public static void main(String args[])
{
SuperTest b=new SuperTest();
b.display();
}
}
Output:-
>java SuperTest
/*program without super keyword*/
● Save the file as SuperTest.java.
Output:-
>java SuperTest
50
/*program without super keyword*/
class Bike
{
int speed=50;
}
class SuperTest2 extends Bike
{
int speed=100;
void display()
{
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[])
{
SuperTest2 b=new SuperTest2();
b.display();
}
}
Output:-
{
int speed=50;
}
class SuperTest2 extends Bike
{
int speed=100;
void display()
{
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[])
{
SuperTest2 b=new SuperTest2();
b.display();
}
}
● Save the file as SuperTest2.java.
Output:-
>java SuperTest2
100