This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding Constructor

 
Dipti Mishra
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

While studying I came across this question.

Which statement is true?

Select the one correct answer.

1) A super() or this() call must always be provided explicitly as the first statement in the body of a constructor.

2) If both a subclass and its superclass do not have any declared constructors, the implicit default constructor of the subclass will call super() when run.

3) If neither super() nor this() is declared as the first statement in the body of a constructor, then this() will implicitly be inserted as the first statement.

4)If super() is the first statement in the body of a constructor, then this() can be declared as the second statement.

5)Calling super() as the first statement in the body of a constructor of a subclass will always work, since all superclasses have a default constructor.

As per what I have studied option (1) seems equally rasonable to me.But when i checked the answer it says (2)

Can anyone tell me where am i mistaken?

Regards,
Dipti
 
Craig Tyler
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 1 is wrong because if you do not add a call to super() or this() explicitly, then a call to super() is implicitly provided for you.

Thus, option 2 is correct since the implicity default constructor will have an implicit call to super() as it's first (and only) statement.
 
Dipti Mishra
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Craig,

Thanks for the clarification.

Regards,
Dipti
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic