GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes illegal combination of modifiers: abstract and static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "illegal combination of modifiers: abstract and static" Watch "illegal combination of modifiers: abstract and static" New topic
Author

illegal combination of modifiers: abstract and static

del boy
Greenhorn

Joined: May 09, 2002
Posts: 6
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
From the JLS, §8.4.3.1 abstract Methods:

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


SCJP Tipline, etc.
del boy
Greenhorn

Joined: May 09, 2002
Posts: 6
Hi Corey,
thanks for your reply.
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.
del boy
Greenhorn

Joined: May 09, 2002
Posts: 6
If static method cannot be ovverridden why I don't get an error message when I try to do so ?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
static methods can not be overridden only hidden. The difference is that hidden methods do not participate in polymorphism. Only overridden methods can participate in polymorphism.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
Don't get me started about those stupid light bulbs.
 
subject: illegal combination of modifiers: abstract and static