The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing with Interfaces



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


No abstract static methods

Posted by Bill Venners on September 18, 1999 at 9:48 PM

> Is their anyway (through either inheritance or interfaces)
> to declare an abstract static method? I would like
> to use polymorphism for several classes and I would
> like all of them to have static method.

> I know I could do this by having the static method (not
> abstract) in the super class and then overriding the
> method in the subclasses, but I would like to have the
> method abstract so it would be required to rewrite the
> method to create the subclass.

I'm afraid you can't always get what you want. Java has no
abstract static methods. Abstract methods are bound up in
the polymorphism thing. They are intended to provide
templates for subclasses not to simply "rewrite" but to
"override" -- so that when they are invoked from a superclass
reference type, dynamic binding causes the appropriate
subclass implementation of the method to run. Static methods
are bound at compile time based on the type of the reference,
not at run time based on the class of the object. When you
rewrite a static method in a subclass, you aren't "overridding"
the superclass version of the method, but simply "hiding" it.
But if you invoke the method on a superclass type reference to
the subclass object, you'll still just get the superclass
implementation of the method. Even though your holding a
reference to a subclass object, the static method
implementation to invoke is decided at compile time based on
the type of the reference, which is the superclass.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us