Super Keyword in Java
In Java Super Keyword, The super keyword in Java serves as a reference variable for the object of the immediate parent class. The super reference variable refers to an implicit instance of the parent class that is created when a subclass instance is created. Java uses the super keyword as a reference variable to refer to objects from parent classes. Understanding the super keyword requires knowledge of inheritance and polymorphism. With the idea of inheritance, the word "super" entered the picture.
Subclasses in Java can access superclass members by using the super keyword (attributes, constructors, and methods). Be sure to understand Java inheritance before learning about the super keyword.
Super keyword Uses
- Super can be used to refer to a class instance variable's immediate parent or to invoke a superclass function that the subclass has overridden.
- You can call the immediate parent class method with super or If both the superclass and the subclass have attributes with the same name, to access the superclass's attributes (fields).
- To call the immediate parent class function Object () {[native code]}, use super () or to specifically invoke the parameterized or no-arg function Object () {[native code]} of the superclass from the subclass function Object () {[native code]}.
Use of super with variables
When a base class and derived class share the same data members, this situation arises. The JVM may experience ambiguity in that situation. With the aid of this sample of code, we can better comprehend it. The data member or field of the parent class can be accessed using the super keyword. When the fields of the parent class and the child class match, it is used.
class Ane {
String clr="Blue";
}
class Dg extends Ane {
String clr="Red";
void printClr () {
System.out.println(clr);//prints color of Dg class
System.out.println(super.clr) ;//prints color of Ane class
}
}
class Super T 1{
public static void main (String s []) {
Dg j=new Dg();
j. printClr ();
}}
Output:
Red
Blue
In the previous illustration, the classes Animal and Dog share the property color. The color of the current class will be automatically published if we print the color attribute. To access the parent property, we must use the super keyword.
Use of super with methods
When calling the parent class method, we use this. So, anytime a parent and child class has methods with the same name, we utilize the super keyword to clear up any confusion. This code snippet clarifies the usage of the super keyword in question.
Parent class methods can also be called using the super keyword. If the subclass and parent class both contain the same method, it ought to be used. It is utilized, in other words, if the method is overridden.
class Ane {
void veh (){System.out.println("started...");}
}
class bike extends Ane {
void start (){System.out.println("startingengine...");}
void break (){System.out.println("breaking...");}
void Dest (){
super. start ();
break();
}
}
TestSuper T 2{
public static void main(String s[]){
bikeb=new bike ();
b. Dest ();
}}
Output:
Started……
breaking……
Because precedence is given to local in the example above, if we call the start () function from the Bike class, it will automatically call the start () method of the bike class.
We must utilize the super keyword to invoke the parent class method. In the example, we saw that using the method start () alone would only call the start () of the current class; but, by using the super keyword, the start () of the superclass may also be called.
Use of super with constructors
The parent class function Object() {[native code]} can alternatively be accessed using the super keyword. Another crucial point is that, depending on the circumstance, "super" can call both parametric and non-parametric constructors. The piece of code to illustrate the principle is as follows:
class Ane {
Ane (){System.out.println("creation of animal completed");}
}
class Dg extends Ane {
Dg(){
Super();
System.out.println("creation of dog is completed");
}
}
class Super T 3{
public static void main(String s[]){
Dg j=new Dg();
}}
Output:
creation of animal completed
creation of dog is completed
Using the term "super" in the subclass function Object() { [native code] }, we called the superclass function Object() { [native code] } in the example above.
Important Points
- The Java compiler automatically inserts a call to the superclass's no-argument function Object () {[native code]} if a function Object () {[native code]} doesn't explicitly call one of its superclass constructors.
- A compile-time error will appear if the superclass does not have a no-argument function Object () {[native code]}. There is no issue if the Object is the lone superclass because the object does have such a function Object () {[native code]}.