Static Methods Non Polymorphic

[Voting on JavaDesignFlaws page.]

Java's static methods are non-polymorphic. This means that sub-classes cannot override class methods. This is unfortunately tied to Java's syntactic roots in CeePlusPlus. ObjectiveCee does this so much more cleanly: methods declared as class methods are truly class methods and are inherently polymorphic.

Does this complaint even make sense? The flaw isn't static methods, it's lack of metaclasses that make class methods unworkable. There's no real logical way static methods could be polymorphic as far as I can see, that would require subclassing what?

But Java has metaclasses (Class as a first order citizen); just that the implementation is flawed.

I partially agree with that, but not really. Class as a first order citizen isn't enough, you need to be able to subclass class and have your classes depened on your new metaclass, which can't be done, without that, you can't say java has metaclasses. If you could do that, then you'd put methods into your new metaclass, and they would be polymorphic, and you wouldn't need the static method hack to simulate class methods. You also wouldn't need factories, since the metaclass can now serve as the factory for that class of objects.

If static on a method means a class method, then a sub-class should theoretically be able to over ride the implementation.

A subclass of what? A subclass of the metaclass sure, but not a subclass of the class itself, otherwise it'd be a regular virtual method. If we have a class Car, and a subclass FordCar?, FordCar? should not logically be able to override static methods of Car, that wouldn't make any sense. First off, static on a method doesn't mean class method, since Java doesn't have metaclasses, static simply means shared method, and overriding a shared method would defeat the meaning of shared method, csharp has the same problem.

IMHO, first of all everything in a class's definition, either defines the class itself or defines the instance that can be created using the class, sharing as a concept didn't seem to me to have a meaning in this context. I think Car, FordCar? example is a good one to show that sub-classes should be able to override their parent's. Basically a Car, a normal car would have two headlights but FordCar? may well have 3, e.g. one in the middle of the other two. You can find many similar examples of static overrides in nature, just look at the variety of breeds of dogs, cats. Serkan Kose


EditText of this page (last edited November 10, 2006) or FindPage with title or text search