ProgrammingVery hardProduct-company placement · GATE CS

What is printed?

What is printed?
class A {
    Object get() { return "A"; }
    void set(String s) { System.out.print("A.set "); }
}
class B extends A {
    String get() { return "B"; }
    void set(Object o) { System.out.print("B.set "); }
}
// in main:
A a = new B();
a.set("x");
System.out.print(a.get());
  1. A. A.set B
  2. B. B.set B
  3. C. A.set A
  4. D. B.set A

Correct answer: A. A.set B

B.get() overrides A.get() with a covariant return type, so a.get() gives B. But B.set(Object) has a different parameter type, so it is an overload, not an override; a.set("x") binds to A.set(String) and nothing overrides it.

More OOP with C++ and Java questions

Exams that ask this topic