• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

illegal combination of modifiers: abstract and static

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
del boy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If static method cannot be ovverridden why I don't get an error message when I try to do so ?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic