wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Inheriting static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Inheriting static methods" Watch "Inheriting static methods" New topic
Author

Inheriting static methods

Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Hi,

Is there a way in which I can create a class heirarchy of a utility class and have one of its static function being overridden in the specialized version of that utility class??

Regards,
Mehul.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
No.

Static methods cannot be overridden. They can be hidden meaning that a subclass can have the same signature as it's superclass, but the method run is that of the reference type at compile time and not the object type at runtime.
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Thanks Steven.

Going a step ahead..

Can I enforce a sub-class writer to implement one of the static methods defined within my class?

Regards,
Mehul.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You might step back and ask why you're using static methods. There are a couple reasons to do so ... how good they are is up to the reader to decide. One is for convenience. It's easier to write Utility.method() than new Utility().method(). Another is the class holds shared (aka global) resources.

If static stuff still sounds good, Steven's point about compile time resolution is important. If there exists a Superclass.method() and you want to override Subclass.method() it's hard to do because there may be thousands of references to Superclass.method() already in the code. The only way to shift those clients to Subclass is to change the code.

If the convenience of static methods is your primary reason, you might make a static class with a configurable reference to a non-static object and pass all methods through to the non-static instance.

Now through configuration I can plug in new implementations or swap them on the fly. At work I can switch between a high-performance cache and an instrumented cache that tracks puts, gets, hits, misses, etc. in a running system. Implementations can extend a base class with some abstract methods to force overrides.

Does that help?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Great..!

Got new perspective.

What I am trying to achieve by inheriting utility class is not to inherit the reusable piece of code but to enforce the behaviour.

I am writing a class


All this is fine for the current release. 6 months down the line when we need to add a specialized version of this utility class for the next release, we want to ensure that just by inheriting Utility it should also be mandatory for sub class writer to implement notification(changes).

I am not sure if my requirement is valid..!

Thanks for your response.

Regards,
Mehul.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Mehul Sanghvi:
we want to ensure that just by inheriting Utility it should also be mandatory for sub class writer to implement notification(changes).


Why do you want to ensure that? Where is the need for the notification method coming from?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Why do you want to ensure that? Where is the need for the notification method coming from?


We are implementing the Utitliy class as JMX compliant.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Mehul Sanghvi:


We are implementing the Utitliy class as JMX compliant.


Sorry, I don't know much about JMX. Could you please elaborate?
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Cool..

Well JMX is simply the observer-observable pattern implemented as extended specification!

To be more specific lets take an example:
Utility = PropertyReader (Reads property values from text file).
At the start of the application, application uses this propertyReader to get the configurable values from file and stores them in public static variables. (Current version of propertyReader does not require cache as there are too few values to be stored).

Application also instantiates JMX classes which provide the user with a UI to change to values of the file. When the user changes the value of the file using JMX classes, JMX classes after changing the value notifies the propertyReader which in turn updates the new values in the static variables.
(JMX-controller, file-model, propertyReader-view).

For this to happen, as soon as the application starts and instantiates the singleton object of propertyReader, that singleton object needs to be registered with JMX classes so that incase of change in property values during the runtime JMX classes can notify propertyReader.

All is fine uptill here.

The current implementation of propertyReader does not require cache implementation but 6-12 months down the line a cache implementation maybe required. At that point of time we may write a NewPropertyReader extends PropertyReader and give different implementation to its utility methods.

What I am trying to find is a way in which I could through my current design make it mandatory for the sub-class writer to implement the static method through which the application currently registers the singleton object to JMX classes.

Thanks for your interest and replies.

Regards,
Mehul.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
In answer to your question: no, there's no way to enforce this programatically in Java.

When I want to do that, I put in a comment saying "subclasses should implement ...". It's also a good idea to comment nonstatic methods that should be overridden as well, so the writer of the subclass knows what the method is supposed to do.
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Thanks Warren.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheriting static methods
 
Similar Threads
DTOs with public final fields
Private Constructors
Utility Classes in EJB
Session Utility class
Static reference to my remote service implementation to avoid java.rmi.NoSuchObjectException