• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending a Singleton class

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am curious what is extending a Singelton class means and how to do it. Suppose I have a Singleton class like this.



Suppose if I want to extend this Singleton class,

I cannot extend it in the 1st place since the constructor is private. So if I make it protected, I can access all the methods of the Singleton class from multiple child class instances and I lose my Singleton property. How do I preserve the Singleton property in the Singleton_Subclass? Do I have to make the constructor private or protected/package private in case some class wants to inherit Singleton_Subclass?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's impossible.

You can have a singleton subclass but then the parent can not be singleton.

And this is an absolutely ridiculous property to have in the first place. If you want only one instance of a type in your program, that should be the responsibility of the type's CLIENT, not the type itself.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't even understand the requirements. Are you allowed one each of the Singleton and Singleton_Subclass types? Or only one, but it can be either of the two types? Or something else? But really, like Stephan I don't have much respect for the requirement, whatever it is.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sriniketh Kovin wrote:I am curious what is extending a Singelton class means and how to do it.


I wonder if this is a "requirement" as much as a "can this be done?" question, and the answer is: NO; for all the reasons you've been given.

The fact is that singletons (like reflection) are almost always evil, and should be avoided if you possibly can.

And if someone is telling you to do this, then they are:
(a) Wrong.
(b) Wasting a lot of money - who's the programmer here?

Winston
 
Sriniketh Kovin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The subclassing Singleton was not a requirement, but more of a curiosity. I am learning Design Patterns and having a hard time co-relating how these patterns help in a real time development scenario.

So, I wanted to exhaust all the options, pros and cons of using a pattern.

Also, if a Singleton can't be subclassed, can a Singleton class be a subclass of another class/implement an interface? Like multiple Singletons implementing a single interface and a client can choose which singleton instance he wants. Again this is a 'Can this be done?' question.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sriniketh Kovin wrote:Also, if a Singleton can't be subclassed, can a Singleton class be a subclass of another class/implement an interface? Like multiple Singletons implementing a single interface and a client can choose which singleton instance he wants. Again this is a 'Can this be done?' question.


The answer to that is "yes"; although the latter (implementing an interface) is more usual. As for having multiple singletons, the most usual way to do that is with an enum (each instance of an enum is, after all, a singleton), and enums can also implement interfaces. They can't subclass another class though, since they are already a subclass of Enum (java.lang.Enum).

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic