A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Refactoring for Software Design Smells: Managing Technical Debt
this week in the
OO, Patterns, UML and Refactoring
Programmer Certification (SCJP/OCPJP)
Doubt regarding Constructor
Joined: Jan 24, 2006
Jan 30, 2006 04:31:00
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?
Joined: Jan 15, 2006
Jan 30, 2006 04:48:00
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.
Joined: Jan 24, 2006
Jan 30, 2006 04:50:00
Thanks for the clarification.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Doubt regarding Constructor
Question on Construtor
a question about default constructor
Why Is a No-Arg Constructor Required Here?
Q. about constructors (and inheritage)
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015