Polymorphism Structure: Types and Mechanisms
Two Types of Polymorphism
Compile-Time Polymorphism (Static): Method overloading where multiple methods share the same name but differ in parameters. The compiler determines which method to call based on the argument types. This is less relevant to LLD design decisions.
Runtime Polymorphism (Dynamic): Method overriding where a subclass provides a specific implementation of a method declared in its parent. The actual method executed is determined at runtime based on the object's actual type, not the reference type.
Class Diagram Structure
+ getVehicleType(): String
+ getVehicleType(): String
+ getVehicleType(): String
+ getVehicleType(): String
Key Mechanism: Dynamic Dispatch
When you call vehicle.calculateParkingFee(hours), the actual method executed depends on whether vehicle references a Car, Motorcycle, or Truck object at runtime. The reference type is Vehicle, but the object type determines behavior.
Money fee = vehicle.calculateParkingFee(2) // Calls Car's implementation
This enables the Liskov Substitution Principle (LSP): subtypes must be substitutable for their base types without altering program correctness.
Bus class requires no changes to existing code that processes vehicles.