Explain the use of 'super' keyword by giving an example.
- The super() / super(parameters) invokes the respective constructor or a method of the super class.
- Invocation of a superclass constructor must be the first line in the subclass constructor.
Syntax:super.method_name()
- It is used to give a call to a method of the superclass in the base class.
- It is used to access the methods of a parent class.
- It is a non-static method and cannot be used inside the main method in Java.
- It invokes the constructor of the parent class.
- The Outer.super can be used to get current instance of outer class and its parent in Java.
Example:class emp
{
float salary=1000;
}
class department extends emp
{
float salary=2000;
void show()
{
System.out.println("Salary :"+super.salary); //Print base class salary
}
}