File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected constructor in singleton pattern

 
Suraj Behera
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.
have a look at this.
http://faq.javaranch.com/java/SingletonPattern
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic