*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overriding Interface vs. Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overriding Interface vs. Class" Watch "Method Overriding Interface vs. Class" New topic
Author

Method Overriding Interface vs. Class

Neil S Staish
Greenhorn

Joined: Mar 24, 2003
Posts: 11
In the following code class B inherits methA from class A and interface BFacethe. I get a compile error:
The method void methA() declared in class B cannot override the method of the same signature declared in interface BFace. The access modifier is made more restrictive.
It looks like the method in class A is ignored?? I mean priority is given to the method in the interface.
interface BFace {
void methA();
}
class A {
void methA() {
System.out.println("MethA in A");
}
}
class B extends A implements BFace {
void methA() {
System.out.println("MethA in B");
}
}
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Methods declared in interfaces are implicitely public (and abstract). The method you have declared in class A has default access, and is thus more restrictive than the method declared in the interface. This is not allowed by the compiler. If you declare methA() public in class A and B everything will be ok
[ March 27, 2003: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
As an experiment, remove methA from class B. Now the subclass inherits a default-access method from the superclass and a public method from the interface.
The method inherited from the superclass overrides the method inherited from the interface. Of course the method inherited from the interface could not override the method inherited from the superclass.
In this case, in some sense the method in the superclass has priority over the method in the interface.
I used to think overriding applied to methods declared in the subclass. Not necessarily so. An inherited method (from the superclass) can override another inherited method (from the interface).
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I won't have thought that the method inherited from the superclass overrides the method inherited from the interface. Is this not a case of implementing as the interface method is implicitly abstract.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
>>I won't have thought that the method inherited from the superclass overrides the method inherited from the interface.
I wouldn�t have thought that either. But if we try to follow the definitions and the usage of the JLS,

It is possible for a class to inherit more than one method with the same signature.
the method that is not abstract is considered to override, and therefore to implement, all the other methods on behalf of the class that inherits it. JLS 8.4.6.4

>>Is this not a case of implementing as the interface method is implicitly abstract.
The non-abstract method �is considered to� override and therefore implement the interface method.
It�s very curious how the JLS says �is considered to override� instead of just �overrides�. In 8.4.6.1 and 9.4.1 the JLS says �is said to override�.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overriding Interface vs. Class