wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes modifiers for interface 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 "modifiers for interface" Watch "modifiers for interface" New topic
Author

modifiers for interface

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
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


SCJP 1.4
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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 ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Oops, I am sorry, line 2 doesn't give an error.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Thanks Dan and Ron, now I understand the reason, not just remember the rules.
 
Consider Paul's rocket mass heater.
 
subject: modifiers for interface
 
Similar Threads
modifier
Help, exam in 2 days!
what are all the modifiers in java...
Constructors and Access Modifiers
Dan's q1 on constructor from single topic