File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Constructor?? 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 Constructor??" Watch "Private Constructor??" New topic
Author

Private Constructor??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9400
    
    2

Hi Ranchers,

I came across a question and here it goes...

Where and how can you use a private constructor?
The answer given as,
Private constructor can be used if you do not want any other class to instanstiate the object, the instantiation is done from a static public method, this method is used when dealing with the factory method pattern when the designer wants only one controller (fatory method ) to create the object.

I'm not getting what it means??? Are they coming to say that private constructors can be instantiated by subclasses???

Anyone on this??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Gaurav Daga
Greenhorn

Joined: Jun 19, 2006
Posts: 14
Hi,

class A{
private A(){

}
private static A a;
public A static getA(){
if(a==null)
a= new A();

return a;

}
}

See this class carefully.

You will find
1. No class can subclass this class means it will behave like final class.
2. Only one object of this class can be created tht means only one. (ie Singleton class).

Hopefully it will give you a brief insight of Singleton pattern.

Cheers
Gaurav
[ November 05, 2006: Message edited by: Gaurav Daga ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Nowhere in the answer do they talk about subclasses, do they?
Please find some more info on the Factory design pattern here

A short example below:


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9400
    
    2

Hi Ranchers,

I'm reposting my question.....

Where and how can you use a private constructor?
The answer given as,
Private constructor can be used if you do not want any other class to instanstiate the object, the instantiation is done from a static public method, this method is used when dealing with the factory method pattern when the designer wants only one controller (fatory method ) to create the object.

I'm not getting what it means??? Are they coming to say that private constructors cannot be instantiated by subclasses???

For the example below,

code:

class Parent{
int j;
int k;
private Parent(int j, int k){
this.j = j;
this.k = k;
}
}
class Main extends Parent{
public Main(){
super(3,5);
}
public static void main(String args[]){
Main m = new Main();
System.out.println(m.i);
}
}



Now compiling the above program gives me error saying that Parent(int, int) has private access. So which constructor will the base class call when I say Main m = new Main(). Please help me guys??

Anyone on this??
Gaurav Daga
Greenhorn

Joined: Jun 19, 2006
Posts: 14
This means you cannot subclass.
It shows that this class will behave as final class.

I hop you would have gone through my post. :-)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9400
    
    2

Hi Gaurav,

Your Quote,

This means you cannot subclass.
It shows that this class will behave as final class.


I can ofcourse include another default constructor which will be public. so I'm now free to subclass it. Isnt't it? What I want to know is that gievn by Valentin in his above post..."//Never let this constructor be invoked from the outside" So we shuould not invoke the private constructor from outside of that class just like how the private members are visible only to its class.

By the way Gaurav...please check your method definition...
public A static getA(){//i guess you means public static A getA()...
if(a==null)
a= new A();

return a;

}

Thanks guys.
Patrick Mugabe
Ranch Hand

Joined: Jan 08, 2002
Posts: 132
If I get you right, I will try to explain. A private constructor is used when you don't want any class to instantiate that class. That class can not be sub classed because your constructor of the subclass will call super() and fail.

Your code fragment fails on super(3,5) because the Constructor Parent(int j, int k) is private.

Maybe to understand this better, add a static method to this code Fragment.

I have changed your code to better explain the concept:

Save this as Main.java and run it. It will show you how you instatiate a class with a Private constructor and how you access it. Pay attention to the static method inside Parent.

class Parent{
int j;
int k;
private Parent(int j, int k){
this.j = j;
this.k = k;
}

public final static Parent instatiateParent(int j, int k){
return new Parent(j,k);
}
}
public class Main{
public Main(){
}
public static void main(String args[]){
//Main m = new Main();
Parent p = Parent.instatiateParent(3,5);
System.out.println(p.j);
System.out.println(p.k);
}
}

Hope this helps you to understand the concept
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9400
    
    2

Hi Above,

I can understand your explanation. You say that if I have a private constructor, I cannot subclass it. But in the above situation, can I add a public default constructor and subclass it?? Now when I do this, I'm free to instantiate the class that has private constructor?? Isn't it?? Can you shed some light on this please!...

Thanks in advance.
Gaurav Daga
Greenhorn

Joined: Jun 19, 2006
Posts: 14
In this way you can surely make subclass of this class.
But to have only private constructor in class will add discussed feature to the class. I hope you will not get confuse anymore. :-)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9400
    
    2

Guys,

Thanks for all your help. I understood it.
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
Hi ,

Check out this code. This may solve your problem.

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9400
    
    2

Hi Guys,

So using access modifiers such as protected, private, public or default for constructors plays the same role like it plays for any member of a class.

Having a protected constructor means that it(the class having protected constructor or the parent class) can be instantiated from it's subclass in another package.....Am I correct?? and a default constructor means it cannot be instantiated from its subclass which is in another package...Am I again right?? Please correct me on this if I'm wrong???

Thanks in advance.
[ November 06, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
GeeCON Prague 2014
 
subject: Private Constructor??