Java - Explain the use of ‘super’ keyword by giving an example

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
   }
}
Define a final class in java. Explain the purpose of final class
Define a final class in java - A class declared as final cannot be extended further by any other class....
What is an interface? Explain with an example using java
What is an interface? - A class that implements an interface is compelled to implement all the methods that have been declared in the interface...
How can we implement multiple inheritances in java?
Multiple inheritances - Java does not support multiple inheritances. However, interfaces can be used in place of multiple inheritances...
Post your comment