Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default constructor

 
sai kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun Yadav
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
sai kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx a lot corey.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic