public class A {
public String name() { return "A"; }
}
public class B extends A {
public String name() { return "B"; }
}
// Usage:
A obj = new B();
System.out.print(obj.name());What is printed?
- A
AB
- Bcheck_circle
B
- C
Compile error
- D
A
Explanation
Method dispatch in Java is based on the runtime (actual) type of the object, not the declared type. obj refers to a B object, so B's name() is called, printing "B".