aspose file tools*
The moose likes Java in General and the fly likes Protected constructor in singleton pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Protected constructor in singleton pattern" Watch "Protected constructor in singleton pattern" New topic
Author

Protected constructor in singleton pattern

Suraj Behera
Greenhorn

Joined: Aug 26, 2010
Posts: 1
While implementing singleton pattern we have two options.
1. Private constructor. - Can not be instatiated.
2. Protected constructor.- can be instatiated only by subclass.


Give me practical example for the second case i.e what is the use of Protected constructor in sigleton pattern.
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Constructors are not inherited so its just the visibility which matters.

It means your sublcass & other classes in your package can create objects of your Singleton class.

Ranchers,

Please correct me if I am wrong.


Regards,
Avishkar Nikale
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Welcome to JavaRanch.
have a look at this.
http://faq.javaranch.com/java/SingletonPattern


SCJP6.0,My blog Ranchers from Delhi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Avishkar Nikale wrote:Constructors are not inherited so its just the visibility which matters.

It means your sublcass & other classes in your package can create objects of your Singleton class.

Ranchers,

Please correct me if I am wrong.

Sub classes cannot create instances*. It cannot use protected constructors to create instances of the super class. Nor can it call protected methods on instances of the super class. The protected means that it can access the constructor in its own constructor (super(...)) and can call the protected methods on its own instances.

* Unless located in the same package.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

If the superclass constructor is made private then you cannot even extend the class
see below code


this will give compile time error.saying superclass constructor is not visible to subclass.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Suraj Behera wrote:Give me practical example for the second case i.e what is the use of Protected constructor in sigleton pattern.

I dont see any use of this, unless you allow someone to subclass; typically singleton has private constructor.
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Rob Prime wrote:
Sub classes cannot create instances*. It cannot use protected constructors to create instances of the super class. Nor can it call protected methods on instances of the super class. The protected means that it can access the constructor in its own constructor (super(...)) and can call the protected methods on its own instances.

* Unless located in the same package.


Thanks for the clarification Rob
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected constructor in singleton pattern