Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interface - Overridden methods are not inherited

I was reading Java SCJP book by Khalid A. Mughal (for JE6), and in topic 7.6 Interfaces and Page number 313, it is given that

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

I could not quite understand what "Overridden methods are not inherited." means. I tried to do this:

interface A
{
    void abc();
}

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

interface C extends B
{
    void abc();
}

And I did not get any error. What am I not understanding?

like image 734
Shubham Batra Avatar asked Aug 02 '15 17:08

Shubham Batra


People also ask

Are overridden methods inherited?

Overridden methods are not inherited.

Are interface methods inherited?

But unlike classes, interfaces can actually inherit from multiple interfaces. This is done by listing the names of all interfaces to inherit from, separated by comma. A class implementing an interface which inherits from multiple interfaces must implement all methods from the interface and its parent interfaces.

Are interface methods overridden?

The default methods are introduced in an interface since Java8. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.

Can we override a method without inheritance?

If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.


1 Answers

This simply means that overridden methods can have a slightly different signature than the superinterface's methods. For example:

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 did throw a checked exception is not inherited, because it is overridden.


I don't have the context of the entire paragraph, but there's something else related that applies only to implementations and not interfaces, which is that if you override a method without explicitly calling super, the superclass's implementation won't occur.

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(), then it will print both.

like image 176
durron597 Avatar answered Oct 02 '22 23:10

durron597