Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Default constructor

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx a lot corey.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic