• 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

Method Overriding Interface vs. Class

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>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�.
 
Where does a nanny get ground to air missles? Protect this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic