This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Class????

 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across a mock exam which had the question as follows..(Please read the explaination in the end)

The No arguments constructor Test() has "default" access modifier ( i.e with no access modifier ). True or False?
Answer : True
Ok I agree with the answer ..But JUST SEE WHAT EXPLAINATION THE MOCK GIVES::
If the class is declared public, then the default constructor is implicitly given the access modifier public. If the class is declared protected, then the default constructor is implicitly given the access modifier protected and if the class is declared private, then the default constructor is implicitly given the access private.
My question now is CAN A CLASS BE EVER DECLARED PRIVATE OR PROTECTED?? THEN HOW DO WE GET A PROTECTED OR A PRIVATE CONSTRUCTOR??
Any Comments?
Sonir
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get a private or protected constructor by declaring it that way of course.
private Test(){}
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sonir shah:
If the class is declared protected, then the default constructor is implicitly given the access modifier protected and if the class is declared private, then the default constructor is implicitly given the access private.
My question now is CAN A CLASS BE EVER DECLARED PRIVATE OR PROTECTED?? THEN HOW DO WE GET A PROTECTED OR A PRIVATE CONSTRUCTOR??
Any Comments?
Sonir[/QB]

Top-level class can not be declared as protected or private .
this is true in case of nested inner classes.
CMIW
 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello William
But if in the exam if we get a code which has a private or a protected Constructor, then what are we supposed to do? Should we mark it as Compiler error or what??
Sonir
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont understand how the answer as u say is true. and then the given explantaion says that if a class is public then the access modifier of the constructor is public also.
like we saw here the class is public but the constructor has a default access modifier.
i must also say i dont agree with the explanation too much. i can have a public class with a private constructor!
Val any ideas on this?
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My idea is that these mock exam questions aren't rigorously tested!!
But anyway, a public class can have a private constructor, there is no problems with that:

This is a public class with a private constructor. Actually, this is an example of a design pattern called a "singleton" that is used a lot. But as you can see, there is no problem making a private constructor. The only class that can access this constructor is the MyClass class itself. If an outside class wants to instantiate a MyClass object, it must use the static getInstance() method of MyClass.
Rob
 
michael opto
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, if a super class has a private constructor, its subclass won't access this private constructor, right?
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is correct. If you need to create a subclass for a class and still want to protect the constructor so that outside classes can't instantiate it, but your subclass still can, you would use the protected access modifier for the constructor, instead of private.

Rob
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by optosheng:
But, if a super class has a private constructor, its subclass won't access this private constructor, right?


constructor are special methods without return type and are not inherited.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic