What specific Object-Oriented Programming (OOP) concept allows different classes to respond to the same method call in their own unique ways, based on their type?
The specific Object-Oriented Programming (OOP) concept that allows different classes to respond to the same method call in their own unique ways, based on their type, is Polymorphism. Polymorphism, meaning "many forms," enables objects of different classes to be treated as objects of a common type, while executing their specific implementations of a shared method. This concept relies on either Inheritance or Interfaces. A base class, which is a blueprint for creating objects, defines a common method. A method is a function associated with an object, defining its behavior. This method in the base class is often declared as virtual or abstract, indicating it can be overridden. Alternatively, an interface, which is a contract specifying a set of methods a class must implement, declares the method signature. Derived classes, which are classes that inherit properties and behaviors from a base class through Inheritance, then override this common method, providing their own distinct implementation tailored to their specific type. Overriding means a derived class provides a specific implementation for a method already defined in its base class. When this common method is called on an object referenced by the common type (e.g., a base class pointer or an interface reference), the system determines which specific overridden method to execute at runtime. This process is known as dynamic method dispatch or late binding. Runtime is the time when a program is executing, in contrast to compile time, which is when source code is translated into machine code. The decision of which method to invoke is based on the actual type of the object, not the declared type of the reference variable. For example, if there is a base class `Shape` with a virtual `draw()` method, and derived classes `Circle` and `Rectangle` each override `draw()` to render themselves uniquely, calling `draw()` on a collection of `Shape` objects will correctly invoke the `Circle`'s `draw()` for `Circle` objects and the `Rectangle`'s `draw()` for `Rectangle` objects, even though they are all treated as `Shape` objects. This facilitates flexible and extensible code.