3

Why is this program giving the output "Class A"?

abstract class A {
      public void abc() {
        System.out.println("Class A");
      }
    }         
    interface B {
      default void abc() {
        System.out.println("Interface B");
      }
    }
    public class Test extends A implements B {
      public static void main(String[] args) {
        Test t = new Test();
        t.abc();
      }
    }

Can anyone explain wh?

John Hascall
  • 9,176
  • 6
  • 48
  • 72

3 Answers3

7

Though the both abstract class and interface have the same method, abstract class method have the high preference while choosing at run time.

Here is the JLS on Method invocation principles

It is possible that no method is the most specific, because there are two or more methods that are maximally specific. In this case:

If all the maximally specific methods have override-equivalent signatures (§8.4.2), then:

If exactly one of the maximally specific methods is concrete (that is, non-abstract or default), it is the most specific method.

Otherwise, if all the maximally specific methods are abstract or default, and the signatures of all of the maximally specific methods have the same erasure (§4.6), then the most specific method is chosen arbitrarily among the subset of the maximally specific methods that have the most specific return type.

In this case, the most specific method is considered to be abstract. Also, the most specific method is considered to throw a checked exception if and only if that exception or its erasure is declared in the throws clauses of each of the maximally specific methods.

Otherwise, the method invocation is ambiguous, and a compile-time error occurs.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Please give an example where there are two maximally specific methods –  Oct 09 '15 at 11:04
  • @UmeshKushwaha Currently you have two maximally specific methods. `abc()` in super class and in your interface `B` another method `abc()` – Suresh Atta Oct 09 '15 at 11:05
  • It's syntax error. Do you have any example where we have two default methods, but one is more specific –  Oct 09 '15 at 11:22
  • @UmeshKushwaha Updated my answer. You hit the last point of the specification :) – Suresh Atta Oct 09 '15 at 11:24
0

When you declare that you are implementing an interface, it is the same as saying:"I will make an implementation for all the methods declared in this interface". When you extends an abstract class you say:"I will implement all the abstract methods in this class and inherits all the non-abstract methods".

So, you inherited the defined method to satisfy the interface' needs, the inheritance override the interface' default.

Mr. M
  • 62
  • 1
  • 15
-2

Edit: I have learnt something new today. Abstract methods have higher preference while choosing at run time.

An interface is a collection of abstract methods. So the

System.out.println("Interface B");

statement is not inherited. The

System.out.println("Class A");

is what is inherited in the instance of Test t, as it is abstract sand has higher preference.

Husman
  • 6,819
  • 9
  • 29
  • 47