• 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

Static Hiding

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In JLS 8.4.6.2 it is given If a class declares a static method ,then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and the superinterfaces of the class that would otherwise be accessible to the code in the class.
The interface methods are implicitly abstract.static modifier cannot be applied to abstract methods.If this is the case, how come a static method in a class can hide a method with same signature in the superinterface?
Thanks,
vinu.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent point! I can't think of a situation where it's possible for a static method to hide a method with the same signature in the superinterface. When I try, the compiler error is:
MyClass.java:14: m1() in MyClass cannot implement m1() in SuperInterface; overriding method is static

If anyone can figure how the JLS is right about this, I'll be much obliged. If not, I'll chalk it up to a careless oversight in the JLS.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you're correct: there is no way for a static method to legally hide a method of the same signature in a superinterface. However this isn't a problem with the JLS. Consider the fuller context from JLS2 8.4.6.2 (or JLS3 8.4.8.2 which says basically the same thing):

If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.


The first sentence defines the term hide - that doesn't necessarily imply that hiding is always legal. The second sentence specifies a particular circumstance in which hiding is not legal. All interface methods are instance methods, and so when it says a static method can't hid an instance method, that eliminates hiding of any interface methods. If they hadn't included interfaces in the first sentence, then the second sentence would not have applied, and we'd be left wondering whether or not it's legal to declare a static method with the same signature as a superinterface method.
[ May 01, 2005: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi for interfaces

methods----->abstract and instance method(non-static)
variables--->Final and Static.

am i right
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Parameswaran Thangavel:
hi for interfaces

methods----->abstract and instance method(non-static)
variables--->Final and Static.



Fields/variables declared in an interface are implicitly public, static and final, and methods are implicitly public and abstract.

From JLS2 9.3 and 9.4:


9.3
Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

9.4
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

Every method declaration in the body of an interface is implicitly public.


[ May 02, 2005: Message edited by: Joyce Lee ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic