Method Overriding
Methods marked with open in the parent class can be overridden by subclasses. When overriding, subclasses must use the override keyword, which both clarifies intent and lets the compiler help check for spelling errors. Methods without the open marker cannot be overridden; attempting to override will result in a compilation error.
Overridden methods maintain the same signature as the parent class (function name, parameters, return type). At runtime, even when called through a parent class reference, it automatically dispatches to the subclass version—this is dynamic dispatch, the core mechanism of object-oriented polymorphism.
The Circle and Square classes below both override area() and label(). When the base class's describe() calls these methods, the runtime correctly invokes the subclass versions, calculating their respective areas and labels.
The dual constraints of open and override both ensure clear design intent and prevent accidental overriding of parent class methods.
Try removing the open modifier from the area() method in the Shape class and observe the compilation error.