๐This() & Super()
Constructors are special methods used to initialize objects in Java. When you create an instance of a class, its constructor gets called to set up the object.
Within a constructor, you can call:
this()
- To call another constructor from the same class. It allows constructor chaining, reusing initialization logic.super()
- To call the parent class constructor. It executes the parent's constructor before the child constructor body.a
Limit
There is no limit on how many times super()
can be called in a constructor. However, there are some restrictions on using this()
:
this()
can only be used to call another constructor in the same class.this()
can only be called once in a constructor.this()
must be the first statement in a constructor.
For example:
So in summary:
super()
can be called multiple timesthis()
can only be called once and must be the first statement
The reason for the this()
restriction is to avoid infinite recursion by constructors repeatedly calling each other in a loop.
Additional Restrictions
There are a couple additional restrictions on using this()
in constructors:
Cannot use
this()
andsuper()
together - It's either one or the other as the first statement.Cannot call
this()
from a default no-arg constructor - Since the no-arg constructor is the default, it doesn't make sense to callthis()
from it.Cannot call private constructors -
this()
can only call visible constructors in the same class.Cannot call constructors of other classes -
this()
can only reference other constructors in the same class.
Some examples:
So in summary, the main additional restrictions are:
Can't use both
this()
andsuper()
Can't call from default constructor
Can't call private constructors
Can't call constructors of other classes
The this keyword refers to the current object instance. It is used to differentiate between attributes/methods of the current object and its parent class.
For example, if a subclass shares a variable name with its parent class, this.variable refers to the subclass variable, while super.variable refers to the parent class variable.
The this keyword is also commonly used to pass the current object instance as a parameter to constructors or methods. Like this.method().
The super keyword is used to reference parent class properties/methods from a subclass. It allows inheriting and extending the parent class.
For example, a subclass may want to call or extend the constructor of its parent class. This uses super() or super(parameters) inside the subclass constructor.
Super can also be used to explicitly call a parent method implementation if the subclass does not override it.
So,
this refers to current object
super refers to parent class
this distinguishes between subclass/parent members
super calls parent class code from subclass
Difference Between this and super keyword
The current instance of the class is represented by this keyword.
The current instance of the parent class is represented by the super keyword.
In order to call the default constructor of the current class, we can use this keyword.
In order to call the default constructor of the parent class, we can use the super keyword.
It can be referred to from a static context. It means it can be invoked from the static context.
It can't be referred to from a static context. It means it cannot be invoked from a static context.
We can use it to access only the current class data members and member functions.
We can use it to access the data members and member functions of the parent class.
Difference Between this() and super() Constructor
The this() constructor refers to the current class object.
The super() constructor refers immediate parent class object.
It is used for invoking the current class method.
It is used for invoking parent class methods.
It can be used anywhere in the parameterized constructor.
It is always the first line in the child class constructor.
It is used for invoking a super-class version of an overridden method.
It is used for invoking a super-class version of an overridden method.
Last updated