ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
interface Greeter {
    default String hello() { return "Hi"; }
}
class Formal implements Greeter {
    @Override
    public String hello() { return Greeter.super.hello() + ", good day"; }
}
// in main:
Greeter g = new Formal();
System.out.println(g.hello());
  1. A. Hi
  2. B. Hi, good day
  3. C. Compile-time error: an interface's default method cannot be called via super from a class
  4. D. Compile-time error: Greeter.super is only valid inside another interface

Correct answer: B. Hi, good day

InterfaceName.super.method() is valid syntax inside an implementing class whenever it needs to explicitly invoke that interface's default implementation. Formal.hello() calls Greeter's default ("Hi") and appends ", good day", so g.hello() (dispatched dynamically to Formal's override) returns "Hi, good day".

More OOP with C++ and Java questions

Exams that ask this topic