aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default constructor" Watch "Default constructor" New topic
Author

Default constructor

Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
source : Enthuware

Which of the following constructors MUST exist in SuperClass for SubClass to compile correctly?


given answers:
public SuperClass(int a);
public SuperClass();


Why do we need the default constructor in this case? The only constructor that the subclass uses of superclass is the one that takes an int. Not sure why we need the no-arg constructor for the subclass point of view.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Got it!
The subclass constructor SubClass(int, int), since it is not explicitly calling the super class constructor, a default no-arg constructor of the superClass will be inserted in order for it to get constructed and that's why the need for the no-arg constructor.
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Thats great man!!!


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by M Krishnan:
... The subclass constructor SubClass(int, int), since it is not explicitly calling the super class constructor, a default no-arg constructor of the superClass will be inserted in order for it to get constructed and that's why the need for the no-arg constructor.

Just to be clear: Without an explicit call to super (or this), an implicit call to the superClass no-args constructor will be inserted in the subclass constructor.

The compiler will add a default no-args constructor to the superClass only if no other constructors are defined. Otherwise, it is left to the programmer to write the no-args constructor.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default constructor