Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

modifiers for interface

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you give reason for why these modifiers are not allowed in interface? Thanks.
static
native
synchronized
final
I have tried this, line1 - line5 gave compiler error
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All of these (except "abstract") are meaningless in an interface, which is why they aren't allowed. An interface has only method declarations, not actual method implementations.
[ November 01, 2002: Message edited by: Ron Newman ]
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I am sorry, line 2 doesn't give an error.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The synchronized and native modifiers specify implementation details that an overriding method is free to change. For example, you can override a synchronized method with a non-synchronized method and a native method can be overridden with a non-native method. Since the superclass can not enforce an implementation detail such as synchronized and native it doesn't make any sense to allow the use of such modifiers in an abstract method declaration.
The "final" modifier can not be used within an abstract method declaration because it would prevent the implementation of the method in a subclass.
An instance method in a superclass can be overridden by a method in the subclass that shares the same signature. The reference to an instance method is resolved at run time. A reference to an abstract superclass or interface will reference the subclass implementation of an abstract method. The same is not true for static methods. A static subclass method does not override a superclass method. Instead, the subclass method hides the static superclass method. Rather than resolve the method reference at run time, a reference to a static method is resolved at compile time. Consequently, a reference to an abstract super class will reference the superclass method. If the method were both static and abstract, then no implementation of the method would be available. For that reason, static methods may not be abstract.
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan and Ron, now I understand the reason, not just remember the rules.
 
reply
    Bookmark Topic Watch Topic
  • New Topic