Creational Patterns «Prev Next»

Concrete Subclasses - Exercise

Objective: Write four concrete subclasses of the Vehicle class.

In the previous lesson, you learned the Factory Method structure and how concrete products (subclasses) sit “below the water line.” In this exercise, you will implement four concrete Vehicle subclasses: Car, Bus, Bicycle, and Pedestrian.

Subclass Requirements

Each subclass should call the protected Vehicle constructor and set length and maxSpeed to the values below. Use the same units consistently (meters for length, kilometers per hour for max speed).

  1. Car: length 6.0 meters, max speed 120 km/h
  2. Bus: length 18.0 meters, max speed 92 km/h
  3. Bicycle: length 1.5 meters, max speed 25 km/h
  4. Pedestrian: length 0.0 meters, max speed 4 km/h

What to implement

  • Define each class and implement a constructor that sets the required values. (For Java, that means calling super(length, maxSpeed).)
  • You do not need additional behavior for this exercise—just correct subclass construction.

Type or paste your solution in the text area below, then click Submit.