views:

3359

answers:

6

The question is in Java why can't I define an abstract static method? for example

abstract class foo {
    abstract void bar( ); // <-- this is ok
    abstract static void bar2(); //<-- this isn't why?
}
+15  A: 

You can't override a static method, so making it abstract would be meaningless. Moreover, a static method in an abstract class would belong to that class, and not the overriding class, so couldn't be used anyway.

GaryF
Yes it is really a shame by the way that static methods cannot be overridden in Java.
Michel
@Michel: what would be the point? If you want instance based behavior, use instance methods.
Ran Biron
This answer is incorrect. Static methods in abstract classes work fine and are commonly used. It's just that a static methods of the class itself may not be abstract. @Michel it doesn't make sense to override a static method. Without an instance, how would the runtime know which method to invoke?
erickson
@erickson - Even without an instance, the class hierarchy is intact - inheritance on static methods can work just like inheritance of instance methods. Smalltalk does it, and it is quite useful.
Jared
+38  A: 

Because "abstract" means: "Implements no functionality", and "static" means: "There is functionality even if you don't have an object instance". And that's a logical contradiction.

Tomalak
nice short answer, thanks :) it wasn't homework though ;)
hhafez
Feel free to roll back my edit. :)
Tomalak
A more concise answer would be 'bad language design.' Static should mean 'belongs to the class' because that's how it's used intuitively as this very question demonstrates. See "classmethod" in Python.
Alexander Ljungberg
@Alexander: This is question not about `static`, but about `abstract static`. Static means "beongs to the class" in Java as well.
Tomalak
@Tomalak I apologize, I was not clear. Of course a static method 'belongs to the class'. Still, it is only in the sense that it lives in the same namespace. A static method is not a method of the class object itself: it does not operate with 'this' as the class object, and it does not participate properly in the chain of inheritance. If it truly was a class method `abstract static` would make perfect sense. It'd be a method of the class object itself which subclass objects must implement. Of course, the way things stands your answer is correct despite my griping about the language.
Alexander Ljungberg
It's not a logical contradiction, it's a language shortcoming, multiple other languages support this notion."abstract" mean "implemented in subclasses", "static" means "executed on the class rather than class instances"There is no logical contradiction.
Eric Grange
@Eric: And *still*, what you say does not apply to `abstract static`: A function X that is "implemented in the subclass" *cannot* at the same time be "executed on the class" - only on the *subclass*. Where it then is not abstract anymore.
Tomalak
+2  A: 

Though the language is different (but the basic idea is same), you can get the "why" here

Learning
A: 

A static method can be called without an instance of the class. In your example you can call foo.bar2(), but not foo.bar(), because for bar you need an instance. Following code would work:

foo var = new ImplementsFoo();
var.bar();

If you call a static method, it will be executed always the same code. In the above example, even if you redefine bar2 in ImplementsFoo, a call to var.bar2() would execute foo.bar2().

If bar2 now has no implementation (that's what abstract means), you can call a method without implementation. That's very harmful.

Mnementh
+2  A: 

The abstract annotation to a method indicates that the method MUST be overriden in a subclass.

In Java, a static member (method or field) cannot be overridden by subclasses (this is not necessarily true in other object oriented languages, see SmallTalk.)

Since static members cannot be overriden in a subclass, the abstract annotation cannot be applied to them.

As an aside - other languages do support static inheritance, just like instance inheritance. From a syntax perspective, those languages usually require the class name to be included in the statement. For example, in Java, assuming you are writing code in ClassA, these are equivalent statements (if methodA() is a static method, and there is no instance method with the same signature):

ClassA.methodA();

and

methodA();

In SmallTalk, the class name is not optional, so the syntax is (note that SmallTalk does not use the . to separate the "subject" and the "verb", but instead uses it as the statemend terminator):

ClassA methodA.

Because the class name is always required, the correct "version" of the method can always be determined by traversing the class hierarchy. For what it's worth, I do occasionally miss static inheritance, and was bitten by the lack of static inheritance in Java when I first started with it. Additionally, SmallTalk is duck-typed (and thus doesn't support program-by-contract.) Thus, it has no abstract modifier for class members.

Jared
+2  A: 

Poor language design. It would be much more effective to call directly a static abstract method than creating an instance just for using that abstract method. Especially true when using an abstract class as a workaround for enum inability to extend, which is another poor design example. Hope they solve those limitations in a next release.

lxndr