ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
class Animal {
    void makeSound() { System.out.print("..."); }
}
class Dog extends Animal {
    @Override
    void makeSound() { System.out.print("Woof"); }
}
class Puppy extends Dog {
    @Override
    void makeSound() { super.super.makeSound(); }
}
What happens when compiling Puppy?
  1. A. It compiles and Puppy.makeSound() calls Animal's version directly
  2. B. Compilation error: Java has no `super.super` syntax to skip an intermediate class
  3. C. It compiles and calls Dog's version
  4. D. It compiles only if Dog is declared abstract

Correct answer: B. Compilation error: Java has no `super.super` syntax to skip an intermediate class

Unlike some languages, Java provides no mechanism to reach a grandparent's method directly — `super.super.method()` is simply invalid syntax and fails to compile; you can only call the immediate super's version with `super.method()`.

More OOP with C++ and Java questions

Exams that ask this topic