super keyword in java
The super keyword in java is a reference variable that is used to refer parent class objects. It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword.
Super keyword is not used in static method.
Various scenarios of using the java super Keyword are as follows:-
- super is used to refer immediate parent instance variable
- super is used to call parent class method
- super() is used to call immediate parent constructor
Super keyword is used to refer immediate parent class object .
Super can be used to invoke immediate parent class method.
The super keyword in java is a reference variable that is used to refer parent class objects.The keyword super came into picture with the concept of inheritance.Whenever we create the instance of subclass,an instance of parent class is created implicitly which is referred by super reference variable.
Usage of Java super keyword:
1.super can be used to refer immediate parent class instance variable.
2.super can be used to invoke are immediate parent class method.
3.super() can be used to invoke immediate parent class constructor.
It is used in the following context:
1.Use of super with variables:This scenario occurs when a derived class and base class has same data members.In that case there is a possibility of ambiguity for the JVM.
2.Use of super with methods:This is used when we want to call parent class method.so whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword.
3.Use of super with constructors: super keyword can also be used to access the parent class constructor.One more important thing is that super can call both parametric as well as non parametric constructors depending upon the situation.