ProgrammingVery hardGATE CS · TCS Digital · Advanced

What is printed?

What is printed?
interface A { default String m() { return "A"; } }
interface B extends A { default String m() { return "B"; } }
interface C extends A { }
class D implements B, C { }
class E implements B, C {
    public String m() { return B.super.m() + "!"; }
}
// in main:
System.out.print(new D().m() + new E().m());
  1. A. compile error
  2. B. BA!
  3. C. BB!
  4. D. AB!

Correct answer: C. BB!

B.m overrides A.m, so A.m is not inherited through C in the presence of B; D simply inherits B.m and there is no conflict. E calls B's default explicitly with B.super.m().

More OOP with C++ and Java questions

Exams that ask this topic