Thursday, September 11, 2008

Abstract Static Methods

They don't exist!

You cannot declare static methods as abstract !

Why Not Abstract:
Static methods are bound at compile-type to the Type of the class. That would make it impossible to ever invoke that declared abstract method. Hence there is no point declaring it in the first place.

Why No Polymorphism:
Some might argue that they want it declared as static abstract in the base class to force subclasses to implement it. And I go back to the same reasoning. You cannot invoke the derived implementation through the base type. You need to use the derived type. Then why bother declaring it in the base class. Static methods do not exhibit polymorphic behavior. It doesn't make sense. Derived classes can redefine and hide base class definitions. Statics are invoked through the Type. What does polymorphism mean in such a scenario. There is no ambiguity to resolve. Polymorphism is displayed by instance variables where we disambiguate its true type at runtime. With statics there is no ambiguity. Everything is sorted out at compile time.
Static methods are statically linked. That is why they are....static :).
Wanting polymorphism in static members is like asking for a banana and wondering why it doesn't taste like an apple ;-)

Friday, September 5, 2008