Java super Keyword: Usage and Examples
Table of Content:
The super keyword in java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.Usage of java super Keyword
- super can be used to refer immediate parent class instance variable.
- super can be used to invoke immediate parent class method.
- super() can be used to invoke immediate parent class constructor.
1) super is used to refer immediate parent class instance variable.
We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.
Program:class Furniture{ String color="Red"; } class Chair extends Furniture{ String color="black"; void printColor(){ System.out.println(color);//prints color of Chair class System.out.println(super.color);//prints color of Furniture class } } class MainClass{ public static void main(String args[]){ Chair d=new Chair(); d.printColor(); } }Output:
black Red Press any key to continue . . .
In the above example, Furniture and Chair both classes have a common property color. If we print color property, it will print the color of current class by default. To access the parent property, we need to use super keyword.
Use of super with variables:
This scenario occurs when a derived class and base class has same data members. In that case there is a possibility of ambiguity for the JVM. We can understand it more clearly using this code snippet:
Program:/* Base class vehicle */ class Vehicle { int maxSpeed = 150; } /* sub class Car extending vehicle */ class Car extends Vehicle { int maxSpeed = 170; void display() { /* print maxSpeed of base class (vehicle) */ System.out.println("Maximum Speed: " + super.maxSpeed); } } /* Driver program to test */ class MainClass { public static void main(String[] args) { Car small = new Car(); small.display(); } }Output:
Maximum Speed: 150 Press any key to continue . . .
In the above example, both base class and subclass have a member maxSpeed. We could access maxSpeed of base class in sublcass using super keyword.
2) super can be used to invoke parent class method
The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.
Program:class Furniture{ void color(){ System.out.println(" Furniture color..."); } } class Chair extends Furniture{ void color(){ System.out.println(" Chair color..."); } void height(){ System.out.println(" 5 ft ..."); } void work(){ super.color(); height(); } } class MainClass{ public static void main(String args[]){ Chair obj=new Chair(); obj.work(); } }Output:
Furniture color... 5 ft ... Press any key to continue . . .
In the above example Furniture and Chair both classes have color() method if we call color() method from Chair class, it will call the Chair() method of Dog class by default because priority is given to local. To call the parent class method, we need to use super keyword.
Use of super with methods:
This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. This code snippet helps to understand the said usage of super keyword.
Program:/* Base class Person */ class Person { void message() { System.out.println("This is person class"); } } /* Subclass Student */ class Student extends Person { void message() { System.out.println("This is student class"); } // Note that display() is only in Student class void display() { // will invoke or call current class message() method message(); // will invoke or call parent class message() method super.message(); } } /* Driver program to test */ class MainClass { public static void main(String args[]) { Student s = new Student(); // calling display() of Student s.display(); } }Output:
This is student class This is person class Press any key to continue . . .
In the above example, we have seen that if we only call method message() then, the current class message() is invoked but with the use of super keyword, message() of superclass could also be invoked.
3) super is used to invoke parent class constructor.
The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:
Program:class Furniture{ Furniture(){ System.out.println("Furniture class Constructor"); } } class Chair extends Furniture{ Chair(){ super(); System.out.println("Chair class Constructor"); } } class MainClass{ public static void main(String args[]){ Chair d=new Chair(); } }Output:
Furniture color... 5 ft color ... Press any key to continue . . .
Note: super() is added in each class constructor automatically by compiler if there is no super() or this().
As we know well that default constructor is provided by compiler automatically if there is no constructor. But, it also adds super() as the first statement.
example of super keyword where super() is provided by the compiler implicitly. Program:
class Furniture{ Furniture(){ System.out.println("Furniture class constructor"); } } class Chair extends Furniture{ Chair(){ System.out.println("Chair class constructor"); } } class MainClass{ public static void main(String args[]){ Chair d=new Chair(); } }Output:
Furniture class constructor Chair class constructor Press any key to continue . . .
Use of super with constructors:
super keyword can also be used to access the parent class constructor. One more important thing is that, 'super’ can call both parametric as well as non parametric constructors depending upon the situation. Following is the code snippet to explain the above concept:
Program:/* superclass Person */ class Person { Person() { System.out.println("Person class Constructor"); } } /* subclass Student extending the Person class */ class Student extends Person { Student() { // invoke or call parent class constructor super(); System.out.println("Student class Constructor"); } } /* Driver program to test*/ class MainClass { public static void main(String[] args) { Student s = new Student(); } }Output:
Person class Constructor Student class Constructor Press any key to continue . . .
In the above example we have called the superclass constructor using keyword ‘super’ via subclass constructor.
Other Important points:
- Call to super() must be the first statement in Derived(Student) Class constructor.
- If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
- If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that a whole chain of constructors called, all the way back to the constructor of Object. This, in fact, is the case. It is called constructor chaining...