Interface - overridden methods are not inherited

I read the Java SCJP book by Khalid A. Mogul (for JE6), and section 7.6, “Interfaces and Page Number 313,” states that

The subinterface can override the abstract method declarations from its superinterfaces. Overridden methods are not inherited.

I could not understand that "Overridden methods are not inherited." . I tried to do this:

interface A { void abc(); } interface B extends A { @Override void abc(); } interface C extends B { void abc(); } 

And I have not received any errors. What? I do not understand?

+6
source share
1 answer

It just means that overridden methods may have a slightly different signature than superinterface methods. For instance:

 public interface Foo { Object doSomething(String input) throws IOException; } public interface Bar extends Foo { @Override String doSomething(String input); } 

As you can see, in the subinterface, I no longer throw a checked exception, and I guarantee that the returned object is a more specific type. The method that throws the checked exception is not inherited because it is overridden.


I don’t have the context of the whole paragraph, but there is something else that applies only to implementations, not to interfaces, which means that if you override the method without explicitly calling super , the superclass implementation will not be executed.

For example, if I have:

 public class Example { public static class Foo { public void printSomething() { System.out.println("Foo"); } } public static class Bar extends Foo { @Override public void printSomething() { System.out.println("Bar"); } } public static void main(String[] args) { Bar bar = new Bar(); bar.printSomething(); } } 

This program will simply output:

 Bar 

but NOT Foo . If I add a call to super.printSomething() , it will print both.

+2
source

All Articles