Hi, I can't quite figure out why I get the following error: illegal combination of modifiers: abstract and static with the follwoing sample code:
or I get a similar erro when: modifier static not allowed here with the following code:
Why is like that ? Why can't I declare in my abstract or interface class a method to be implemented or extended as static by subclasses ? This rule really baffles me.... Am I missing something ? Thanks
It is a compile-time error for a static method to be declared abstract.
An abstract method is defined only so that it can be overridden in a subclass. However, static methods can not be overridden. Therefore, it is a compile-time error to have an abstract, static method. Corey
An abstract method is defined only so that it can be overridden in a subclass. However, static methods can not be overridden. Therefore, it is a compile-time error to have an abstract, static method.
I am getting confused here... The following code compiles fine (and it definitely overrides the superclass meth) and prints the message defined in the meth of the sub-class ... It looks to me that subclass can override static superclass methods. Many thanks in adbvance.
Joined: May 09, 2002
If static method cannot be ovverridden why I don't get an error message when I try to do so ?
Joined: Dec 20, 2001
You're not overriding the method, you're hiding it. Try this:
As you can see, dynamic binding is only used with the instance method, not the static method. That's because only the instance method is overridden. The static method is hidden. There is a distinct difference. Check out the JLS, §8.4.6 Inheritance, Overriding, and Hiding for more details. Corey