Super keyword in Java
Super Keyword in java used to refers the super class object. super keyword is a reference variable in java programming language . super keyword used in inheritance.Note : We can not use the super keyword inside the static area .
Super Keyword Usage in java
Important usages of super keyword in java as follows
- Super keyword is used to call the super class instance variables.
- Super keyword is used to call the super class method.
- Super keyword is used to call the super class constructor.
Use (1) : Super keyword is used to call the super class instance variables.
Syntax
class Bike
{
String brand="Hero-Honda";
}
class Car extends Bike
{
String brand="Maruti";
void brandName()
{
System.out.println(brand);
System.out.println(super.brand);// Use of super keyword
}
}
class Drive
{
public static void main(String args[])
{
Car d=new Car();
d.brandName();
}
}
Output : Maruti
Hero-Honda
Use (2) : Super keyword is used to call the super class method
Syntax
Output : Car speed is 140 kmphUse (2) : Super keyword is used to call the super class method
Syntax
class Bike
{
void speed()
{
System.out.println("Bike speed is 100 kmph");
}
}
class Car extends Bike
{
void speed()
{
System.out.println("Car speed is 140 kmph");
}
void checkSpeed()
{
speed();
super.speed();
}
}
class FinalSpeed{
public static void main(String args[])
{
Car d=new Car();
d.checkSpeed();
}
}
Bike speed is 100 kmph
Use (3) : Super keyword is used to call the super class constructor.
Rule :
1-Super keyword must be first statement inside the constructor.
2- Inside the constructor we can not use both super keyword and this keyword.
3- Inside the Constructor we can not use 2 super keyword .
Syntax
class Bike
{
Bike()
{
System.out.println("Speed is 80 Kmph");
}
}
class Car extends Bike
{
Car()
{
super();
System.out.println("Speed is 120 kmph");
}
}
class SpeedTest
{
public static void main(String args[])
{
Car d=new Car();
}
}
Output : Speed is 80 kmph
Speed is 120 kmph
Note : When we not declare any super keyword inside the constructor ?
If we are not declaring any super keyword inside the constructor then the compiler generate the super keyword with 0-argument .Compiler generated super keyword not visible to user.
Syntax
class Bike
Note : When we not declare any super keyword inside the constructor ?
If we are not declaring any super keyword inside the constructor then the compiler generate the super keyword with 0-argument .Compiler generated super keyword not visible to user.
Syntax
class Bike
{
Bike()
{
System.out.println("Speed is 80 Kmph");
}
}
class Car extends Bike
{
Car()
{
super(); //Compiler generated super keyword
System.out.println("Speed is 120 kmph");
}
}
class SpeedTest
{
public static void main(String args[])
{
Car d=new Car();
}}
Tags : Super keyword in java , java super keyword , usages of java super keyword , example of super keyword
Output : Speed is 80 kmph
Speed is 120 kmph
Important Points : Super Keywords
Hope!!! The above lesson on java super keywords is helpful for you ...
Important Points : Super Keywords
- To call parent class instance variable use super keyword.
- To Call Parent class method use Super keyword
- To Call Parent Class constructor use super keyword .
- Super keyword must be first statement in constructor .
- We can declare only one super keyword inside the the constructor .
- We can not declare both this keyword and super keyword inside the constructor .
- We can not used the super keyword inside the static area.
Hope!!! The above lesson on java super keywords is helpful for you ...
Technology Team,
UPCS
UP Consultancy Services
Tags : Super keyword in java , java super keyword , usages of java super keyword , example of super keyword