super keyword in java

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.

There are three ways to utilize "super" keyword. 

  • To refer super class variables ( To Access Parent Class Variables )
  • To refer super class constructors ( To Call Parent Class Constructor )
  • To refer super class methods. ( To Access Parent Class Methods )

To Access Parent Class Variables

`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.

Code Example
Output
Brand from Car: Car Brand
Brand from Vehicle: Vehicle Brand

Output

50 60
10 20
100 100

To refer super class constructors

`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.
Code Example
Output
A-int-param-Con 
B-Con

 

Important Points About `super` keyword
  • 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.
Output
A-con 
B-int-param-con 
C-int-param-con 

To Access Parent Class Methods

`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.

Code Example
Output
This is a vehicle.
This is a car.