Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Constructor??

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Gaurav Daga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Thanks for all your help. I understood it.
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Check out this code. This may solve your problem.

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic