File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Extending a Singleton Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Extending a Singleton Class" Watch "Extending a Singleton Class" New topic
Author

Extending a Singleton Class

sree harsha.hn
Greenhorn

Joined: Jun 18, 2009
Posts: 7
Hi,
Can somebody explain the concept of extending a Singleton Class?

Thanks,
Harsha
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
Ideally singleton class have private constructor.
We can override a class having private constructor beocz super constructor will not be accessible.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Usually you wouldn't extend a Singleton class. In fact, usually a Singleton class would be designed in a way to prevent it being extended, in case extending it was used as a way to create multiple instances.
bernard amadeus
Greenhorn

Joined: May 15, 2009
Posts: 21
Matthew Brown wrote:Usually you wouldn't extend a Singleton class. In fact, usually a Singleton class would be designed in a way to prevent it being extended, in case extending it was used as a way to create multiple instances.

I think that the definition of a Singleton as having only one instance is misleading: better say that only one combination of values (object "state") is involved in methods implementation.
So curiously you can create a Singleton that is implemented without the calling code being aware of this property: the different instances created by client code all delegate to a single object!
and yes you can create subclasses of this façade.
sree harsha.hn
Greenhorn

Joined: Jun 18, 2009
Posts: 7
Hi All,
Thanks for your responses.

I still didnt get a concrete answer about whether we can Extend a Singleton Class or not.

Could you please post the code snippets?

Thanks,
Harsha
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

you can attempt to do it the exact same way you'd extend any other class...however, depending on how the singleton is written it may not actually work. It is impossible to say without knowing more about the specific singleton implementation you are trying to extend, which clearly we don't have. But all you'd do is



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

All you need to extend a singleton class is a constructor with protected or package-default in the singleton class. If there are only private constructors you simply won't be able to extend it. If there are public constructors then it's not a singleton class.

Note that to use a package-default constructor your class needs to be in the same package as the singleton class; visibility of constructors is no different than the visibility of methods or fields.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Of course, you should never actually do this. Singletons are evil and should be avoided.
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
@Harsha-

Just for sake of my own interest, may I ask you why are looking to extend Singlton class Or where you got this requiremtn from ?

What i understand is Singlton is just a concept that one instance per JVM (or per App in case of App servers like WLS).
you can implement this using different ways,
the most common way I have seen is making Contructior private and providing Factory method to control Instantiation: in this case definitely Extends would never work since constructor is private.

I would be happy to see the Singlton implimentation you have in your mind which allows the Extension (I mean Extends), may be i would learn something more

thanks!


“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
Pat Farrell wrote:Singletons are evil and should be avoided.

can you please elaborate on this please? Since actually I found them quite handy sometimes
 
GeeCON Prague 2014
 
subject: Extending a Singleton Class