• 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: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can somebody explain the concept of extending a Singleton Class?

Thanks,
Harsha
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ideally singleton class have private constructor.
We can override a class having private constructor beocz super constructor will not be accessible.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, you should never actually do this. Singletons are evil and should be avoided.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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!
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic