ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
class Base {
    public String describe() { return "Base"; }
}
interface Describable {
    String describe();
}
class Sub extends Base implements Describable {
}
public class Main {
    public static void main(String[] args) {
        Describable d = new Sub();
        System.out.println(d.describe());
    }
}
  1. A. Compile-time error: Sub must explicitly implement describe()
  2. B. Base
  3. C. Compile-time error: describe() is ambiguous between Base and Describable
  4. D. null

Correct answer: B. Base

An inherited concrete method can satisfy an interface's abstract method requirement automatically, as long as the signatures (and return type) match, without Sub needing to redeclare it. Sub inherits Base.describe() and that satisfies Describable, so d.describe() returns "Base".

More OOP with C++ and Java questions

Exams that ask this topic