• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to override static method. Singleton is hard to subclass.  RSS feed

 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to subclass a sigleton and I want the instance to be created of derrived class but not singleton. I've tried to make

public static Parent newInstance();

and override it in derrived class, but that doesn't work since you can't override static method.

Best regards
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you can override a static method. We do it all the time when we create a main method in subclasses.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you mean that you can't extend the Singleton because its default constructor is private?
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above example works but if the Singleton class zero parameter constructor is private it doesn't.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Technically static methods are not overridden, they're just shadowed like member fields.

You can always access a static method in any class using SomeClass.staticMethod(). That doesn't work for methods that are overridden due to dynamic method invocation at runtime.

Jules
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rovas, no we don't override them. There is no polymorphism for variables (class members) nor static methods. And we don't override main. I think it's called using reflection.

Oh ok. The problem with a singleton...



Even if constructor is protected there is a problem. Code



As you can see it always creates MyParentSingleton doesn't matter if method is called for MyChildSingleton. I thought to make static method makeInstance() which would be overriden in MyChildSingleton. This method would return new object of correct type (MyParentSingleton for parent and MyChildSingleton for child). Alas this dont work because static methods are not overriden but shadowed. This was my problem. I've read OOD book, there is no easy/elegant solutions. Singletons are hard to subclass.

Best regards
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for pointing that out - learn something everyday. Vladas, why is it that you can't call MyChildSingleton.getInstance() or call getInstance on a reference of type MyChildSingleton?
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rovas Kram:
Thanks guys for pointing that out - learn something everyday. Vladas, why is it that you can't call MyChildSingleton.getInstance() or call getInstance on a reference of type MyChildSingleton?


How can the MyParentSingleton class know what the caller wants? Maybe the caller of the MyParentSingleton.getInstance() wants the MyParentSingleton instance and not MyChildSingleton.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How can the MyParentSingleton class know what the caller wants? Maybe the caller of the MyParentSingleton.getInstance() wants the MyParentSingleton instance and not MyChildSingleton.


I have no idea how your statement relates to my statement. Maybe there's some implied context that I didn't get from the original post. Please elaborate.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of ways to handle this. Here's a factory which can create any subclass of Singleton. You can set the name of the class it creates using the setClassname method. Instead of providing a method like this, more likely the class would read it from a properties file, or other configuration mechanism.

Error handling is left as an exercise for the reader

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!