ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
interface Flyer { default String move() { return "fly"; } }
interface Swimmer { default String move() { return "swim"; } }
class Duck implements Flyer, Swimmer {
    public String move() { return Flyer.super.move() + "+" + Swimmer.super.move(); }
}
// in main:
System.out.println(new Duck().move());
  1. A. fly
  2. B. swim
  3. C. fly+swim
  4. D. Compile-time error: cannot call two different interfaces' super methods in one expression

Correct answer: C. fly+swim

Duck resolves the diamond conflict by explicitly invoking both parents' defaults with InterfaceName.super.method(), concatenating their results, so move() returns "fly+swim".

More OOP with C++ and Java questions

Exams that ask this topic