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

Private Constructors

 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If a class has private no-arg constructors, those classes cannot be instantiated? True/ False
"TRUE"
If a class has private constructors, those classes can be subclassed, but the constructors defined in subclasses cannot invoke super constructors ?True/False
"True"
If a class has no_arg private constructors, but if it has other constructors which are private, then the subclass can instantiate the class with out any compiler error?
True/false
True
I just want to know is my ideas clear. Also I would like to know what all the things we need know on AWT and events for exam. Is it necessary to learn each every method in every class. What we have to know on I/O and threads?
 
Maitham H
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the last part of your questions, try to carefully read and understand the objectives of the exam as stated in the sun web site, they give you good idea on what to read and concentrate on.
 
Jim Hall
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class has private no-arg constructors, those classes cannot be instantiated? True/ False
"TRUE"

FALSE
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arathi,
your first statment is mostly correct, but I would say that "If a class has a private no-arg constructor, that class cannot be instantiated by any classes using the no_arg constructor."
If there are other non-private constructors, those can be used to instantiate the class. here's a simple example:

Hope this helps
Rob
[ January 08, 2002: Message edited by: Rob Ross ]
 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Hope this helps
Rob
[ January 08, 2002: Message edited by: Rob Ross ][/QB]
The above code works fine. But if you subclass a class, then it gives compiler error. Am i right. Because the subclass will invoke super class constructor and since the super class constructor is private it gives compiler error. Are you getting what i am mentioning. In your code, the class is not subclassed. So it won't be problem when you compile p=new Private("String");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic