What is super keyword in java with example?
In Java, the
super keyword is a reference variable used to refer to the immediate parent class object. It is used inside a subclass method or constructor to call a method or constructor of the superclass.
Here are the key uses of the
1. Invoke Superclass Methods:
supercan be used to invoke methods of the immediate parent class. If a subclass has a method with the same name as a method in its superclass, using
superallows you to explicitly call the method from the superclass.
2. Invoke Superclass Constructor:
superis also used to call the constructor of the immediate parent class. This is particularly useful when the subclass constructor needs to perform additional tasks after calling the parent class constructor.
3. Access Parent Class Members:
supercan also be used to access the data members of the parent class when they are hidden by the child class. This is often used when the child class has a variable with the same name as a variable in the parent class, and you need to access the parent class variable.
In summary, the
super keyword in Java is used to differentiate between superclass and subclass members, allowing you to access or call the members of the immediate parent class in the context of the subclass.