super keyword in java
1. "super" in java
super
is an implicit reference variable. This reference variable is available to all the sub-classes. super
is created implicitly during the initialization of the sub-class and it holds the reference of the super-class of the initialized sub-class.
2. Uses of "super" in java
super has the following uses in java:
2.1. To invoke the constructor of super-class from sub-class.
super
can be used to invoke the constructor of the super-class from the sub-class constructor.
Here we have defined a super-class with just a default constructor in it.
class SuperClass {
public SuperClass(){
System.out.println("Default constructor of the super class is invoked");
}
}
Now we will extend the super-class by creating a sub-class
class SubClass extends SuperClass{
public SubClass(){
super();
System.out.println("Default constructor of the sub class is invoked");
}
public static void main(String arr[]){
SubClass obj = new SubClass();
}
}
Output:
Default constructor of the super class is invoked
Default constructor of the sub class is invoked
As you can see that the constructor of the super-class was invoked from the constructor of the sub-class due to the presence of super()
in the sub-class constructor.
You can also invoke the parameterized constructor of the super-class by providing the arguments to the super()
in the sub-class constructor. For example:
Following is the super-class constructor we want to invoke from the sub-class:
public SuperClass(String msg){
System.out.println("Message received by the super class constructor is : "+msg);
}
To invoke this constructor we need to pass the argument to the super() in the sub-class constructor.
class SubClass extends SuperClass{
public SubClass(String msg){
super(msg);
System.out.println("Constructor of the sub class is invoked");
}
public static void main(String arr[]){
SubClass obj = new SubClass("String argument");
}
}
Output:
Message received by the super class constructor is : String argument
Constructor of the sub class is invoked
Note: super()
must be the first statement in the constructor of the sub-class in order to invoke the super-class constructor.
2.2. To invoke the method of super-class
super
can be used to invoke the method of a super-class from a sub-class method by using super
.
Suppose we the following method in the super-class
public void m1(){
System.out.println("Method of super class invoked");
}
To invoke method m1()
we need to use super.m1()
in sub-class method.
class SubClass extends SuperClass{
public void m2(){
super.m1();
System.out.println("Method of sub class is invoked");
}
public static void main(String arr[]){
SubClass obj = new SubClass();
obj.m2();
}
}
Output:
Method of super class invoked
Method of sub class is invoked
2.3. To refer data member of super-class
super
can be used to refer to the data member of super-class and use it in the sub-class.
Suppose we have the following super-class with a data member in it
class SuperClass {
String msg = "This is a super class variable";
}
We can use the super-class variable in sub-class as following:
class SubClass extends SuperClass{
public void m2(){
System.out.println(super.msg+" in sub class method");
}
public static void main(String arr[]){
SubClass obj = new SubClass();
obj.m2();
}
}
Output:
This is a super class variable in sub class method
3. Conclusion
"super" is very much like this in java. The only difference is that this
stores the reference of the instance of the current class and super
stores the reference of the super-class.