Overriding Methods

AP Computer Science A· difficulty 3/5

public class A {
    public Number get() { return 1; }
}
public class B extends A {
    public Integer get() { return 2; }
}
A x = new B();
System.out.println(x.get());

What is printed?

  • A

    Compile-time error

  • B

    Runtime error

  • C

    2

    check_circle
  • D

    1

Explanation

Dynamic dispatch invokes B.get() returning 2.

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

Related questions