Vehicle Transportation System
Inheritance hierarchy for different vehicle types with parking management
classDiagram
class Vehicule {
<<abstract>>
-int engine
#String brand
+Vehicule(String brand, int engine)
+start()* void
+stop()* void
}
class Plane {
-boolean trainAtterissageActif
+Plane(String brand, int engine)
+start() void
+stop() void
+sortirTrainAtterissage() void
+rentrerTrainAtterissage() void
+fly() void
}
class Car {
-int nbDoor
+Car(String brand, int engine, int nbDoor)
+start() void
+stop() void
}
class Parking {
-String name
-List~Vehicule~ vehicles
+Parking(String name)
+add(Vehicule v) void
+startAllVehicles() void
}
class Boat {
+Boat(String brand, int engine)
+start() void
+stop() void
+sail() void
}
Car --|> Vehicule
Plane --|> Vehicule
Boat --|> Vehicule
Parking "1" --> "0..*" Vehicule
Vehicule (Abstract Base Class)
Section titled “Vehicule (Abstract Base Class)”The parent abstract class for all vehicle types.
- engine: Engine size or power (private)
- brand: Vehicle brand name (protected - accessible to subclasses)
- Vehicule(String brand, int engine): Constructor to initialize brand and engine
- start(): Abstract method - must be implemented by subclasses
- stop(): Abstract method - must be implemented by subclasses
- Protected brand allows subclasses to access the brand information
- Cannot be instantiated directly - serves as a template for concrete vehicle types
Car vehicle with doors.
- nbDoor: Number of doors (private attribute)
- Car(String brand, int engine, int nbDoor): Constructor accepting brand, engine, and door count
- Inherits from Vehicule
- Overrides start() and stop() for car-specific behavior (e.g., “Car engine starting…”)
Aircraft with landing gear mechanism.
- trainAtterissageActif: Landing gear active status (private boolean)
- Plane(String brand, int engine): Constructor that calls super() and initializes landing gear to false
- start(): Overrides to check if landing gear is deployed before starting
- stop(): Overrides to ensure safe landing procedures
- sortirTrainAtterissage(): Deploy landing gear (sets trainAtterissageActif to true)
- rentrerTrainAtterissage(): Retract landing gear (sets trainAtterissageActif to false)
- fly(): Airplane-specific flight method (can only fly if landing gear is retracted)
- Inherits from Vehicule
Watercraft with sailing capability.
- Boat(String brand, int engine): Constructor that calls super() with brand and engine
- start(): Overrides to start boat engine
- stop(): Overrides to stop boat engine
- sail(): Boat-specific sailing method for water navigation
- Inherits from Vehicule
Parking
Section titled “Parking”Manages a collection of vehicles.
- name: Parking name (private attribute)
- vehicles: List of vehicles in parking (private
List<Vehicule>) - Can contain zero or more vehicles (one-to-many relationship with
Vehicule) - Parking(String name): Constructor to initialize parking name and empty vehicles list
- add(Vehicule v): Add a vehicle to the parking lot
- startAllVehicles(): Iterate through all parked vehicles and call their start() method
- Demonstrates polymorphism - can store any type of
Vehicule(Car, Plane, Boat)
Practice Exercise: Implement the Vehicle System
Section titled “Practice Exercise: Implement the Vehicle System”Objective: Implement the complete vehicle transportation system with abstract base class and polymorphism.
Requirements:
- Create the abstract
Vehiculebase class with abstract methods - Implement all three vehicle classes (Car, Plane, Boat) extending Vehicule
- Override abstract methods with specific behavior for each vehicle type
- Implement the Parking class that manages a collection of vehicles
- Demonstrate polymorphism by storing different vehicle types in the same parking
💡 Click to see suggested implementation
// Abstract base classpublic abstract class Vehicule { private int engine; protected String brand;
public Vehicule(String brand, int engine) { this.brand = brand; this.engine = engine; }
// Abstract methods - must be implemented by subclasses public abstract void start(); public abstract void stop();
public int getEngine() { return engine; } public String getBrand() { return brand; }}
// Car classpublic class Car extends Vehicule { private int nbDoor;
public Car(String brand, int engine, int nbDoor) { super(brand, engine); this.nbDoor = nbDoor; }
@Override public void start() { System.out.println("Car " + brand + " with " + nbDoor + " doors: Engine starting..."); }
@Override public void stop() { System.out.println("Car " + brand + ": Engine stopping..."); }
public int getNbDoor() { return nbDoor; }}
// Plane classpublic class Plane extends Vehicule { private boolean trainAtterissageActif;
public Plane(String brand, int engine) { super(brand, engine); this.trainAtterissageActif = false; // Retracted by default }
@Override public void start() { if (trainAtterissageActif) { System.out.println("Plane " + brand + ": Engines starting. Landing gear deployed."); } else { System.out.println("WARNING: Landing gear not deployed!"); } }
@Override public void stop() { System.out.println("Plane " + brand + ": Engines stopping. Prepare for landing."); }
public void sortirTrainAtterissage() { trainAtterissageActif = true; System.out.println("Plane " + brand + ": Landing gear deployed."); }
public void rentrerTrainAtterissage() { trainAtterissageActif = false; System.out.println("Plane " + brand + ": Landing gear retracted."); }
public void fly() { if (!trainAtterissageActif) { System.out.println("Plane " + brand + ": Flying!"); } else { System.out.println("ERROR: Cannot fly with landing gear deployed!"); } }}
// Boat classpublic class Boat extends Vehicule {
public Boat(String brand, int engine) { super(brand, engine); }
@Override public void start() { System.out.println("Boat " + brand + ": Engine starting for navigation..."); }
@Override public void stop() { System.out.println("Boat " + brand + ": Engine stopping. Dropping anchor..."); }
public void sail() { System.out.println("Boat " + brand + ": Sailing on the water!"); }}
// Parking classimport java.util.ArrayList;import java.util.List;
public class Parking { private String name; private List<Vehicule> vehicles;
public Parking(String name) { this.name = name; this.vehicles = new ArrayList<>(); }
public void add(Vehicule v) { vehicles.add(v); System.out.println("Vehicle added to " + name + " parking."); }
public void startAllVehicles() { System.out.println("\n=== Starting all vehicles in " + name + " ==="); for (Vehicule v : vehicles) { v.start(); // Polymorphism - calls the appropriate start() method } }
public String getName() { return name; } public int getVehicleCount() { return vehicles.size(); }}
// Example usagepublic class VehicleDemo { public static void main(String[] args) { // Create parking Parking parking = new Parking("City Center Parking");
// Create different types of vehicles Car car1 = new Car("Toyota", 2000, 4); Car car2 = new Car("BMW", 3000, 2); Plane plane = new Plane("Boeing", 50000); Boat boat = new Boat("Yamaha", 500);
// Add vehicles to parking (polymorphism) parking.add(car1); parking.add(car2); parking.add(plane); parking.add(boat);
// Start all vehicles using polymorphism parking.startAllVehicles();
System.out.println("\n=== Plane-specific operations ==="); plane.sortirTrainAtterissage(); plane.fly(); // Won't work with gear deployed plane.rentrerTrainAtterissage(); plane.fly(); // Now it works
System.out.println("\n=== Boat-specific operations ==="); boat.sail(); }}Key Implementation Points:
- Use
abstractkeyword for Vehicule class and its methods - All subclasses MUST implement abstract methods start() and stop()
- Use
@Overrideannotation when overriding methods - Call
super(brand, engine)in subclass constructors - Parking uses
List<Vehicule>to demonstrate polymorphism - Abstract classes cannot be instantiated directly (cannot do
new Vehicule(...))

