wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Class???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Class????" Watch "Private Class????" New topic
Author

Private Class????

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
You get a private or protected constructor by declaring it that way of course.
private Test(){}
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
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


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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

Joined: Jan 13, 2002
Posts: 732
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

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
michael opto
Greenhorn

Joined: Jan 17, 2002
Posts: 29
But, if a super class has a private constructor, its subclass won't access this private constructor, right?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Oct 15, 2001
Posts: 5371
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Private Class????