In an inheritance relationship, the subclass inherits all functionalities of the parent class, which possesses all attributes, methods, and subclasses.
classDiagram
Car <|-- Bus
Car <|-- Taxi
class Car {
-name
+run()
}
class Bus {
}
class Taxi {
}
Implementation
In an implementation relationship, a class implements an interface, and the methods within the class implement all the methods declared by the interface.
classDiagram
Car <|.. Bus
Car <|.. Taxi
class Car {
+run()
}
class Bus {
+run()
+XFunc()
}
class Taxi {
+run()
+YFunc()
}
Composition
The relationship between the whole and its parts, where the whole and parts cannot be separated
classDiagram
class Car {
-Wheel[] wheels
-Chassis chassis
+drive()
}
class Wheel {
-int diameter
-String material
+rotate()
}
class Chassis {
-String material
-String color
+support()
}
Car *-- Wheel : "4"
Car *-- Chassis
Aggregation
The relationship between the whole and its parts, where the whole and parts can be separated.
classDiagram
class Car {
+DustCover dustCover
+SnowChain[] snowChains
+drive()
}
class DustCover {
-String material
-String size
+protect()
}
class SnowChain {
-int size
-String material
+install()
}
Car o-- DustCover
Car o-- SnowChain : "4"
Association
Indicates that one class holds a reference to one or more instances of another class as its attribute.
classDiagram
class Driver {
-Car[] cars
+addCar(car: Car)
}
class Car {
-String driver
+setDriver(driver: string)
}
Driver "1" --> "0..*" Car
Dependency
If changes in class A(Car) lead to changes in class B(Oil), then class B is said to depend on class A.
classDiagram
class Car {
+beforeRun(oil: Oil)
}
class Oil {
-Int type
+add()
}
Car <.. Oil : depends on