The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. It is used to access parent class members (variables and methods) that are hidden by the child class.
`super` keyword can be used to access parent class variables. We will utilize super keyword, to access super class variables when we have same set of variables at local, at current class and at the super class.
class Vehicle { protected String brand = "Vehicle Brand";}class Car extends Vehicle { private String brand = "Car Brand"; public void displayBrand() { System.out.println("Brand from Car: " + brand); System.out.println("Brand from Vehicle: " + super.brand); // Accesses the parent class variable }}public class Main { public static void main(String[] args) { Car car = new Car(); car.displayBrand(); }}Brand from Car: Car Brand
Brand from Vehicle: Vehicle Brandxxxxxxxxxxclass A { int i=100; int j=200; } class B extends A { int i=10; int j=20; B(int i, int j) { System.out.println(i+" "+j); System.out.println(this.i+" "+this.j); System.out.println(super.i+" "+super.i); } } public class PlanetLearning { public static void main(String args[]) { B b = new B(50,60); } }50 60
10 20
100 100`super` can be used to call the parent class constructor.
In general, JVM will execute 0-argument constructor before executing sub class constructor. In this context, instead of executing 0-argument constructor we want to execute a parameterized constructor at super class, for this, we have to use 'super' keyword.xxxxxxxxxxclass A { A() { // super(); by default compiler will place this method here System.out.println("A-Con"); } A(int i) { // super(); by default compiler will place this method here System.out.println("A-int-param-Con"); } } class B extends A { B() { super(10); System.out.println("B-Con"); } } class Test { Test() { System.out.println("Test-Con"); } public static void main(String args[]) { B b = new B(); } } A-int-param-Con
B-Con
If we want to access super class constructor from subclass by using "super" keyword then the respective "super" statement must be provided as first statement. If we want to access super class constructor from sub class by using "super" keyword then the respective "super" statement must be provided in the subclass constructors only, not in subclass normal Java methods. If we violate any of the above conditions then compiler will rise an error like "call to super must be first statement in constructor".
Due to the above rules and regulations, it is not possible to access more than one super class constructor from a single sub class constructor by using "super" keyword, because call to super must be first statement in constructor`In the case of inheritance, when we access sub class constructor then, first, JVM will execute super class 0-arg constructor then JVM will execute sub class constructor, this flow of execution is possible in Java because of the following compiler actions over source code at the time of compilation.
Compiler will go to each and every class available in the source file and checks whether any requirement to provide "default constructors".
If any class is identified without user defined constructor explicitly then compiler will add a 0-argument constructor as default constructor.
After providing default constructors, compiler will go to all the constructors at each and every class and compiler will check "super" statement is provided or not by the developer explicitly to access super class constructor.
If any class constructor is identified with out "super" statement explicitly then compiler will append "super()" statement in the respective constructor to access a 0- argument constructor in the respective super class.
With the above compiler actions ,JVM will execute super class 0-arg constructor as part of executing sub class constructor explicitly.xxxxxxxxxxclass A { A() { // super(); by default compiler will place this method here System.out.println("A-con"); } } class B extends A { B(int i) { // super(); by default compiler will place this method here System.out.println("B-int-param-con"); } }class C extends B { C(int i) { super(10); System.out.println("C-int-param-con"); } } public class Test { public static void main(String args[]) { C c = new C(10); } } A-con
B-int-param-con
C-int-param-con `super` can be used to call parent class methods. In Java applications, when we have same method at both subclass and at super class, if we access that method at sub class then JVM will execute sub class method only, not super class method because JVM will give more priority for the local class methods. In this context, if we want to refer super class method over sub class method then we have to "super" keyword.
xxxxxxxxxxclass Vehicle { public void display() { System.out.println("This is a vehicle."); }}class Car extends Vehicle { public void display() { super.display(); // Calls the parent class method System.out.println("This is a car."); }}public class PlanetLearning { public static void main(String[] args) { Car car = new Car(); car.display(); // Output: This is a vehicle. This is a car. }}This is a vehicle.
This is a car.