Difference between super() and this() in Java
super() and this() both are the keywords used to make constructor calls. super() is used to call the Parent class's constructor and this() is used to call the current class's constructor. Both have their own rules, where they can be used and not mixed with each other.
Table of Content :
- Introduction
- super keyword
- this keyword
- Important points about this() and super()
- Difference between super() and this() in Java
- Java program to demonstrate the Difference between super() and this() in Java
- Questions related to the Difference between super() and this() in Java
- Summary
super keyword
The super keyword in java is a reference variable that is used to refer to parent class objects. The keyword "super" came into the picture with the concept of Inheritance. Basically, this form of super is used to initialize superclass variables when there is no constructor present in the superclass. On the other hand, it is generally used to access the specific variable of a superclass.
The super keyword can also be used to access the parent class constructor by adding '()' after it, i.e. super(). One more important thing is that 'super()' can call both parametric as well as non-parametric constructors depending upon the situation.
this keyword
this is a reserved keyword in java i.e, we can't use it as an identifier.
It is used to refer to current class instances as well as static members.
The following are the different cases where this can be used.
- It is used to refer to the instance variable of the current class.
- It is used to invoke or initiate the current class constructor.
- It can be passed as an argument in the method call.
- It can be passed as an argument in the constructor call.
- It can be used to return the current class instance.
Important points about this() and super()
- We can use super() as well as this() only once inside the constructor. If we use super() twice or this() twice or super() followed by this() or this() followed by super(), then immediately we get compile time error i.e, we can use either super() or this() as first statement inside constructor and not both.
- It is up to you whether you use super() or this() or not because if we are not using this() or super() then by default compiler will put super() as the first statement inside the constructor.
- A recursive constructor call is not allowed using this
public class Test {
Test() {
this(30);
}
Test(int a) {
this();
}
public static void main(String[] args) {
new Test();
}
}
output:
Test.java:16: error: recursive constructor invocation
Test(int a)
^
1 error
Difference between super() and this() in Java
super() | this() |
---|---|
super() - refers immediate parent class instance. | this() refers current class instance. |
super() acts as an immediate parent class constructor and should be the first line in the child class constructor. | this() acts as a current class constructor and can be used in parameterized constructors. |
When invoking a superclass version of an overridden method the super keyword is used. | When invoking a current version of an overridden method this keyword is used |
Can be used to invoke the immediate parent class method. | Can be used to invoke the current class method |
Java program to demonstrate the Difference between super() and this() in Java
In the below program, we can see the clear difference between this() and super()
package com.javacodestuffs.core.java.examples;
class Bird {
String name;
Bird(String name) {
this.name = name;
}
public void fly() {
System.out.println("Birds are able to fly.");
}
public void show() {
System.out.println(name);
}
}
class Herons extends Bird {
Herons() {
// Using this to call current class constructor
this("Herons");
}
Herons(String name) {
// used to call parent constructor
super(name);
}
public void fly() {
// this will call super class method
super.fly();
System.out.println("Herons are able to fly and swim as well.");
}
}
public class BirdTestMain {
public static void main(String args[]) {
// Bird reference but actually its Herons object
Bird b = new Bird("Herons");
b.show();
// call method in Herons class
b.fly();
}
}
output:
Herons
Birds are able to fly.
Questions related to the Difference between super() and this() in Java
Can we have this () and super () together?
no, we can't use super() and this() together.
Should I use super or this?
Can we use this () and super () in a method in Java?
We can use super() as well as this() only once inside the constructor. If we use super() twice or this() twice or super() followed by this() or this() followed by super(), then immediately we get compile time error i.e, we can use either super() or this() as first statement inside constructor and not both
In this article, we have seen the Difference between super() and this() in Java.
0 Comments
Post a Comment