• Post Reply Bookmark Topic Watch Topic
  • New Topic

The super and this keywords in subclass constructors  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The super keyword when used explicitely in a subclass constructor must be the first statement but what about if i have a this(parameters) statements ? As the this one must also be the first statement... Does this means that i can have only one or the other ? What about when the super constructor is not explicit (aka implicit ) , can i use the this( parameters) in the same constructor ?
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first statement in a constructor must be super OR this. You can't have both.
If you don't provide any, the Java compiler will use implicit one.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the statement is correct :

public class(){
//implicit super()
this(2 ,4);

Only a declared super and this cannot be in the same constructor , a implicit super can exist with a this right ? As all classes have a object class as the top superclass ...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:but the statement is correct :

public class(){
//implicit super()
this(2 ,4);

Only a declared super and this cannot be in the same constructor , a implicit super can exist with a this right ? As all classes have a object class as the top superclass ...

No, the implicit super() will not be there. It is only implicit if there is no super(...) or this(...). The this(...) redirects construction to a different constructor in the same class. That new constructor must either have a super(...) or this(...) as the first line, and if it doesn't then the implicit super() will be added.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no implicit super in a constructor that calls this(2, 4);
An implicit super might be in the other constructor (if this constructor does not call super or this explicitly).
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more complete example:
There is no implicit super() between line 3 and 4. Instead, construction begins with the constructor on line 7. And that constructor, having no explicit super() or this() call, gets an implicit super() call between lines 7 and 8.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't agree with you just yet ...

If i have a single class with one of the constructors starting with this(parameters) and that constructor is invoked with new <constructor with the this statement>.toString() how does it execute the toString method from the Object class ? We all know that the Object class is the super class for any non derived classes . Aka the String class for example .All classes have the top super class the Object class . And you can see that by using the toString() in a class where the toString() method is not defined /overriden , or the equal method....

So i think you can have a this(parameters) statement with a implicit super() ...

Here is one example:



Am i wrong ? Why ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:I don't agree with you just yet ...

If i have a single class with one of the constructors starting with this this(parameters) and that constructor is invoked with new <constructor with the this statement>.toString() how does it execute the toString method from the object class ? We all know that the Object class is the super class for any class that does not have any other superclasses . Aka the String class for example .All classes have the top super class the Object class . And you can see that by using the toString() in a class where the method is not defined , or equal ....

So i think you can have a this() statement with a implicit super() ...


Instantiating an object, and call a method on an instance are two separate things.... so...



Does two things. The first is instantiating the instance, which is the topic being discussed here... And the other is about calling the method, which happens when the instantiation has been completed, and is a discussion for stuff that happens after the stuff being discussed in this topic.

Now, of course, you can call methods in the constructor, which means that there are other rules to follow... but it still doesn't affect the rules being discussed in this topic.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:
Here is one example:



Am i wrong ? Why ?


Well, if you are correct, then the Person constructor should get called twice. Once implicitly just before the this() call in the no-arg Employee() constructor, and once implicitly in the Employee(String) constructor, that was called via the this(String) call. Does the Person constructor message show up twice?

Henry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure why you think calling a method on the returned object says anything about the way constructors were called on that object.

In that example, here's how the implicit super() calls are arranged:
- The Faculty() constructor doesn't have an explicit this() or super(), so super() is implicitly added. This calls the Employee() constructor.

- The Employee() constructor has an explicit this(String) call. Nothing is implicitly added, but the Employee(String) constructor is explicitly called.

- The Employee(String) constructor doesn't have an explicit this() or super(), so super() is implicitly added. This calls the Person() constructor.

- The Person() constructor doesn't have an explicit this() or super(), so super() is implicitly added. This calls the Object() constructor.


So the implicit super() is only added to those constructors that don't have an explicit call.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I'm not quite sure why you think calling a method on the returned object says anything about the way constructors were called on that object.

In that example, here's how the implicit super() calls are arranged:
- The Faculty() constructor doesn't have an explicit this() or super(), so super() is implicitly added. This calls the Employee() constructor.

- The Employee() constructor has an explicit this(String) call. Nothing is implicitly added, but the Employee(String) constructor is explicitly called.

- The Employee(String) constructor doesn't have an explicit this() or super(), so super() is implicitly added. This calls the Person() constructor.

- The Person() constructor doesn't have an explicit this() or super(), so super() is implicitly added. This calls the Object() constructor.


So the implicit super() is only added to those constructors that don't have an explicit call.


If what you said about Employee class is correct it would display the println in the Emplyee constructors before displaying the println in the Person class , and this does not happen if you run the code .

What i said is incorrect as this() is the first statement , your right .

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you realized your mistake in the last post. Even so, for posterity, I will post the execution sequence:

Faculty() called
-- Implicit Employee() called
-- -- Employee(String) called
-- -- -- Implicit Person() called
-- -- -- -- Implicit Object() called
-- -- -- -- -- Object() executed
-- -- -- -- Person() executed
-- -- -- Employee(String) executed
-- -- Employee() executed
-- Faculty() executed

Also, a link to the JLS: Classes>Constructor Declarations>Constructor Body:
the JLS wrote:The first statement of a constructor body may be an explicit invocation of another constructor of the same class or of the direct superclass
...
If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();"
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone for helping me clear that up
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!