File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default constructor?" Watch "Default constructor?" New topic
Author

Default constructor?

Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
The default constructor invokes the no-parameter constructor of the superclass.
true or false?


Val SCJP2 1.2<br />====>SCJD
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Val Lee:
The default constructor invokes the no-parameter constructor of the superclass.
true or false?

True.
Of course, all constructors invoke the no-parameter constructor of the superclass unless a call to a different constructor is made explicitly through the use of the keyword super.
Corey


SCJP Tipline, etc.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
true
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
That's true, but not only the default constructor in the subclass does this. Even if you have a non-default constructor in your sub-class, it will still call the default constructor of the supper class, unless you specify the correct signature of the non-default constructor of the super-class (if there are any) in the constructor of the subclass.
take this example:

e.g

HTH
Edited by Corey McGlone: Broke up the long comment into multiple lines for better display.
[ July 11, 2002: Message edited by: Corey McGlone ]

Amir
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
guys sorry about the messup of putting a long text inside the brackets. didn't realize the side-effect!
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
From the JLS,�8.8.7 Default Constructor
A compile-time error occurs if a default constructor is provided by the compiler but the superclass does not have an accessible constructor that takes no arguments.

So I think it is false.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Val Lee:
From the JLS,�8.8.7 Default Constructor

So I think it is false.

Huh?
That statement says that a compiler error occurs if the superclass doesn't have an accessible no-args constructor. This would only be an error is the default constructor tried to invoke the no-args constructor.
Therefore, this statement is proving the fact that a default constructor invokes the no-args constructor of the superclass, not the other way around.
Try this:

Corey
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Amir yaar what have you done?

Well, I never liked splitting hairs, but IMHO the question was if the superclass constructor will be invoked, not if the invocation was actually successful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Default constructor?
 
Similar Threads
Spring: Hello World Does Not Work
implicit constructor
Constructor of a Servlet ?
this(Color.Red) ?
Which statement is true for default Constructor?