In this article we will look into the super keyword in java. The super keyword in Java play important role in the concept of inheritance.
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.
Java super Keyword
The super keyword in Java is has two form i.e, as Super keyword & Super() also know as “super as constructor“.
- super can be used to refer to a variable of the immediate parent class
- super can be used to invoke the immediate parent class method.
- super() are used in constructor to invoke the immediate parent class constructor.
Characteristics of Super Keyword
The following are the characteristics of the Super Keyword:
- super keyword refers to the parent class of a subclass.
- Method Invocation: “super” can be use to call a method defined in its parent class
- Field Access: super can be used to refer immediate parent class instance variable
- Constructors Invocation: super() “i.e, super as constructor“is the first statement inside the constructor. When creating a subclass, its constructor must invoke the constructor of its parent class using “super()”. This ensures proper initialization of the superclass’s state.
Super keyword 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.
Example
class Car{ String name; Car(String name) { this.name = name; } void applyGear() { System.out.println(name + " Apply Gear in Generic Ways"); } } class Audi extends Car{ String name; Audi (String name) { super(name); // Invoking superclass constructor this.name = "Audi : " + name; } @Override void applyGear() { super.applyGear(); // Call superclass method System.out.println(name + " GEAR"); } } class Example{ public static void main(String[] args) { Audi audi = new Audi ("Audi Car"); audi.applyGear(); } }
Output:
Audi Car Apply Gear in Generic Ways
Audi : Audi Car GEAR
Super keyword 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.
To call the parent class method, we need to use super keyword.
class A{ String message = "Hello from Class A"; void display() { System.out.println(message); } } class B extends A{ String message = "Hello from Class B"; void display() { super.display(); // Invoking the parent class method System.out.println(message); } } class Example{ public static void main(String[] args) { B b = new B(); b.display(); } }
Output:
Hello from Class A
Hello from Class B
Super keyword 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:
class A{ String name; A(String name) { this.name = name; System.out.println("A Class constructor invoked"); } } class B extends A{ int i; A (String s, int i) { super(name); // INVOKING SUPERCLASS CONSTRUCTOR this.i= i; System.out.println("B Class constructor invoked"); } void display() { System.out.println("Name: " + name); System.out.println("Number: " + number); } } class Example{ public static void main(String[] args) { B b = new B("JavaOcean", 123); b.display(); } }
Output:
A Class constructor invoked
B Class constructor invoked
Name: JavaOcean
Number: 123
Automatic super() provided by compiler
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.
This ensures that the superclass is properly initialized before the subclass. This behavior is especially important when you consider that every class implicitly extends the Object class.
If a constructor in a subclass does not have a super(…) or this(…) call, it is assumed to contain an implicit super() call at the beginning.
class Car{ Car() { System.out.println("Car constructor"); } } class BMW extends Car{ BMW (){ // Compiler will insert an implicit super() call here System.out.println("BMW constructor"); } } class SuperExample{ public static void main(String[] args) { BMW bmw= new BMW (); } }