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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default constructor" Watch "Default constructor" New topic
Author

Default constructor

sai kumar
Ranch Hand

Joined: Mar 31, 2004
Posts: 72
Hi all,
Can anyone explain the answer to the following question:
Which three statement are true?
A.The default constructor initialzes method variables.
B. The default constructor has the same access as its class
C.The default constructor invoked the no-arg constructor of the super class.
D. The class lacks a no-arg contructor, the compiler always creates a default construtor.
E. The compiler creates a default constructor only when there are no other constructors for the class.
Answer B,C,E.
Is B a correct statement. Can somone explain...
Thx,
sk
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
The default constructor is supplied automatically by the compiler when no other constructors are specified by the programmer.
The only functionality of the default constructor is to call the constructor from the superclass.
Indeed the default constructor has the same access modifier as the the class. If the class is defined as public, the default constructor is public. If the class is protected, the default constructor is protected, etc.


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Arun Yadav
Greenhorn

Joined: Mar 14, 2003
Posts: 19
Original Quote: Thomas De Vos
***
Indeed the default constructor has the same access modifier as the the class. If the class is defined as public, the default constructor is public. If the class is protected, the default constructor is protected, etc.
***
Thomas, I doubt , if class can be protected ?, Class can be either public or default(package level).


Arun (SCJP-1.4, IBM-141, SCWCD-1.4)
"How hard you try is rooted to how often you try" - Doug Hall"
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
I was referring to member classes enclosed in a top-level class.
Indeed top-level classes cannot be declared as private or protected.
sai kumar
Ranch Hand

Joined: Mar 31, 2004
Posts: 72
Hi Thomas,
Is there a difference between inbuilt default constructor and a default constructor. My understanding was that a default constructor is a constructor without any arguments. If such a constructor has an explicit access modifier (like public or private or ..) which is different from the class modifier then is the answer choice B.( The default constructor has the same access as its class) still valid??
Thx.
sk
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
From the JLS, §8.8.7 Default Constructor:

If the class is declared public, then the default constructor is implicitly given the access modifier public (�6.6); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (�6.6); if the class is declared private, then the default constructor is implicitly given the access modifier private (�6.6); otherwise, the default constructor has the default access implied by no access modifier.

B is a correct answer - the default constructor will have the same access modifier as the class that contains it.
In addition, the default constructor is different than a defined "no-arg" constructor. Check out this recent thread for more details.
Corey
[ March 31, 2004: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
sai kumar
Ranch Hand

Joined: Mar 31, 2004
Posts: 72
Thx a lot corey.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Default constructor