Skip to content

Composition And Encapsulation

classDiagram
    class Car {
        -int speed
        -int maxSpeed
        +Car(CarInfo carInfo)
        +Car(CarInfo carInfo, int speed)
        +Car(serialString Number, String brand, int speed)
        +compare(Car v1, Car v2) String
        +compare(Car v) String
        +accelerate(int speed)
        +decelerate(int speed)
    }
    class Parking {
        -String name
        -String city
        -String address
        +Parking(name, address, city)
        +countByBrand(String brand) int
        +countCars()
        +add(Car c)
    }
    class CarInfo {
        -String serialNumber
        -String brand
        +CarInfo(serial, brand)
    }
    class OwnerInfo {
        -String firstName
        -String lastName
        +OwnerInfo(firstName, lastName)
    }
    CarInfo "1" *-- "1" Car
    Car "1" --> "0..1" OwnerInfo
    Parking "1" --> "0..*" Car

Demonstrates composition and encapsulation.

  • Composition: Car must have CarInfo (shown with filled diamond *--)
  • Association: Car may have OwnerInfo (shown with arrow -->) - zero or one owner
  • Multiple constructors showing different initialization options
  • Private properties have getters and setters
  • Comparison methods to compare cars

Essential information about a car (composition relationship).

  • Cannot exist without a Car
  • Private properties have getters and setters

Information about the car’s owner (association relationship).

  • Can exist independently of Car
  • Private properties have getters and setters

Manages multiple cars (one-to-many association).

  • Methods to count cars and search by brand
  • Private properties have getters and setters