Java Puzzle #10: Multiple Interfaces

· by

You have to following source code:

A.java:

public interface A {
    public int methodA(double a, int b, char c);
    public int methodB();
}

B.java:

public interface B {
    public int methodB();
    public void methodC();
}

test.java:

public class test implements A, B {
    public static void main(String[] args) {
        test t = new test();
        System.out.println(t.methodA(1, 2, '3'));
        System.out.println(t.methodB());
        t.methodC();
    }

    @Override
    public int methodA(double a, int b, char c) {
        return 42;
    }

    @Override
    public int methodB() {
        return 1337;
    }

    @Override
    public void methodC() {
        System.out.println("methodC executed.");
    }
}

What is the output? Does it compile? Is there a RuntimeException?

. . . . . . . . . . . . . . . . .

Answer

Output:

42
1337
methodC executed.

Explanation

If you use an Interface, it simply means you have to implement some methods. If more than one Interface forces you to implement the method, you still have to implement it only once. It just works fine.