Polymorphism

AP Computer Science A· difficulty 4/5

public class A {
    public String show(A o) { return "A-A"; }
}
public class B extends A { }
A x = new A();
B y = new B();
System.out.println(x.show(y));

What is printed?

  • A

    B-A

  • B

    Compile-time error

  • C

    B-B

  • D

    A-A

    check_circle

Explanation

show(A) accepts B (upcast). Calls A.show(A) returning "A-A".

Want 10 more like this — adaptive to your weak spots?

Related questions

AP Computer Science A · Polymorphism Practice Question | Acemy