• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Java Puzzle #10: Multiple Interfaces

Contents

  • Java Puzzle #10: Multiple Interfaces
    • Answer
    • Explanation

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.


Published

Aug 16, 2012
by Martin Thoma

Category

Code

Tags

  • Java 36
  • Programming 52
  • puzzle 22

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor