Creating Superclasses and Subclasses

AP Computer Science A· difficulty 2/5

// Design choice: a Car has an Engine and is a Vehicle.
public class Engine {}
public class Vehicle {}
// Which design is correct?
public class Car extends Vehicle {
    private Engine engine;
}

Which best describes this design?

  • A

    Car has-a Vehicle and has-a Engine

  • B

    Car is-a Vehicle and is-a Engine

  • C

    Car is-a Engine and has-a Vehicle

  • D

    Car is-a Vehicle (inheritance) and has-a Engine (composition)

    check_circle

Explanation

Inheritance (extends) models is-a; an instance field models has-a. Car extends Vehicle (is-a) and contains an Engine field (has-a).

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

Related questions