Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheriting static methods

 
Mehul Sanghvi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mehul Sanghvi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mehul Sanghvi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Warren.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic