Why can't this() and super() both be used together in a constructor?
Solution 1
this(...)
will call another constructor in the same class whereas super()
will call a super constructor. If there is no super()
in a constructor the compiler will add one implicitly.
Thus if both were allowed you could end up calling the super
constructor twice.
Example (don't look for a sense in the parameters):
class A {
public A() {
this( false );
}
public A(boolean someFlag) {
}
}
class B extends A {
public B() {
super();
}
public B( boolean someFlag ) {
super( someFlag );
}
public B ( int someNumber ) {
this(); //
}
}
Now, if you call new B(5)
the following constructors are invoked:
this( false);
A() ---------------> A(false)
^
|
| super();
|
| this();
B() <--------------- B(5) <--- you start here
Update:
If you were able to use this()
and super()
you could end up with something like this:
(Attention: this is meant to show what could go wrong, if you were allowed to do that - which you fortunately aren't)
this( false);
A() ---------------> A(false)
^ ^
| |
| super(); | super( true ); <--- Problem: should the parameter be true or false?
| |
| this(); |
B() <--------------- B(5) <--- you start here
As you can see, you'd run into a problem where the A(boolean)
constructor could be invoked with different parameters and you'd now have to somehow decide which should be used. Additionally the other constructors (A()
and B()
) could contain code that now might not get called correctly (i.e. out of order etc.) since the call to super( true )
would circumvent them while this()
wouldn't.
Solution 2
There is a difference between super()
and this()
.
super()
- calls the base class constructor whereas
this()
- calls current class constructor.
Both this()
and super()
are constructor calls.
Constructor call must always be the first statement. So you either have super()
or this()
as first statement.
Solution 3
Both this()
and super()
are constructor calls, and constructor call must be the first (and only first) call in a constructor. Otherwise, the Object
constructor will be called more than once when instantiating a single object.
Solution 4
- we use this() keyword in constructor chaining to access the constructor of the same class
- we use super() keyword when we want to access the constructor of immediate parent class in inheritance.
And there is a condition in both that they have to be declared in the first line of the constructor you are using. And that's the reason why we can't use both in a single constructor because you can write only one thing in your first line.
Solution 5
Because it doesn't make sense. A constructor has to either call this()
or super()
(implicitly or explicitly). this()
calls another constructor which has to call either this()
or super()
etc as before. A constructor that called both this()
and super()
would therefore ultimately call super()
twice.
Babloo
Updated on July 03, 2021Comments
-
Babloo almost 3 years
Why can't
this()
andsuper()
both be used together in a constructor?What is the reason for incorporating such a thing?
-
Balaswamy Vaddeman about 12 yearsbasically at a time only one statement is used,what do u mean by "we can not have two statements as first statement",it has got no meaning