ProgrammingHardProduct-company MCQ pattern
Interfaces A and B both declare default void hello(). What happens with class C implements A, B { } ?
- A. B's hello is used because it is listed last
- B. It compiles, and calling hello() throws an exception
- C. A's hello is used because A is listed first
- D. Compilation error: C must override hello() (it may call A.super.hello() inside)
Correct answer: D. Compilation error: C must override hello() (it may call A.super.hello() inside)
When two interfaces supply the same default method, Java refuses to guess. C must override hello(), and may delegate to one of them with A.super.hello() or B.super.hello().
More OOP with C++ and Java questions
- How many of these are correct in Java (8 or later)? (i) An abstract class can have a constructor. (ii) An interface can declare instance…
- What is printed? java // static methods of the main class: static void f() { try { System.out.print("1"); throw new…
- What is printed? java String s1 = "Java"; String s2 = "Ja" + "va"; String part = "Ja"; String s3 = part + "va"; System.out.print((s1 ==…
- What is printed? java class P { private void f() { System.out.print("P"); } void g() { f(); } } class C extends P { void f() {…
- What is printed? java class Animal { String speak() { return "animal"; } } class Dog extends Animal { String speak() { return "dog"; } }…
- What is printed? java class P { int id; P(int id) { this.id = id; } @Override public boolean equals(Object o) { return o instanceof P &&…