• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

modifiers for interface

 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Oops, I am sorry, line 2 doesn't give an error.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Dan and Ron, now I understand the reason, not just remember the rules.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic