Creating References Using Inheritance Hierarchies

AP Computer Science A· difficulty 2/5

public abstract class Vehicle {
    public abstract int wheels();
}
public class Bike extends Vehicle {
    public int wheels() { return 2; }
}
// Usage:
Vehicle v;
v = new Bike();
System.out.print(v.wheels());

What is printed?

  • A

    Runtime error

  • B

    Compile error: cannot declare Vehicle reference

  • C

    0

  • D

    2

    check_circle

Explanation

Abstract classes can be used as REFERENCE types even though they cannot be instantiated. v refers to a Bike, and dispatch returns 2.

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

Related questions