24 Oct 2023
Compile-time (Static) Polymorphism and Runtime (Dynamic) Polymorphism are two types of Polymorphism :
-
Compile-time (Static) Polymorphism:
- Method Overloading: In compile-time polymorphism, it is achieved through method overloading. Method overloading allows you to define multiple methods in the same class with the same name but different parameters (number, type, or order).
- Compile-Time Resolution: The appropriate method to be called is determined at compile time by the compiler, based on the number and types of arguments provided in the method call.
Real world Examples of Compile-time (Static) Polymorphism:
-
Door Locks:
Imagine a traditional physical door lock with two different keys:
- House Key:
- This key is designed to open the front door of a house.
- The shape and pattern of the house key are specifically created for that lock.
- Car Key:
- This key is meant to unlock the door of a car.
- It has a unique shape and pattern designed exclusively for the car's lock.
In this example:
- The lock mechanism in both cases (house and car) remains the same.
- The keys (house key and car key) represent different methods of accessing the lock.
- The lock does not adapt or change its mechanism based on the key used.
- The key's design and the lock's mechanism are predetermined and set during the manufacturing process.
- The lock does not make runtime decisions to change its behavior. It always responds to the unique pattern of the key it was designed for.
This illustrates compile-time polymorphism because the behavior of the lock (unlocking) is fixed and determined during the manufacturing or design phase, and it doesn't change at runtime based on the key used.
- House Key:
-
Electrical Plugs and Outlets:
Consider the different types of electrical plugs and outlets used worldwide:
- Type A Plug:
- This type of plug is commonly used in North America and some other regions.
- It has two flat parallel pins.
- Type B Plug:
- Also used in North America, it has two flat parallel pins and a grounding pin.
- Type C Plug:
- Used in many European and Asian countries, it has two round pins.
- Type D Plug:
- Used in India and some neighboring countries, it has three round pins in a triangular configuration.
- Type E Plug:
- Common in France and some other European countries, it has two round pins and a hole for an earthing pin.
- Type F Plug:
- Another European standard, it has two round pins and two grounding clips on the sides.
In this example:
- Electrical outlets have specific designs to accommodate a particular type of plug.
- The configuration and shape of plugs and outlets are fixed and standardized during the manufacturing process.
- A Type A plug will fit into a Type A outlet, and a Type B plug will fit into a Type B outlet. However, a Type A plug will not fit into a Type B outlet.
- The behavior of plugging in a device is predetermined and fixed at compile time based on the types of plugs and outlets.
This represents compile-time polymorphism because the compatibility and behavior of plugs and outlets are determined during the design and manufacturing phases and do not change dynamically at runtime.
- Type A Plug:
- Runtime (Dynamic) Polymorphism:
- Method Overriding: In runtime polymorphism, it is achieved through method overriding. Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
- Dynamic Binding: The method to be executed is determined during runtime based on the actual object's type, not at compile time. This is also known as dynamic binding or late binding.
Real world Examples of Runtime (Dynamic) Polymorphism:
-
Runtime Polymorphism in Cars:
- Cars can be categorized into sedans, trucks, or SUVs.
- All cars share common functions like starting, stopping, and steering.
- Each type of car has unique characteristics, like fuel economy and cargo capacity.
- This is an example of runtime polymorphism because the specific type of car being used is determined during runtime. The shared functions (starting, stopping, steering) are executed differently for each car type based on its unique characteristics.
-
Runtime Polymorphism in Websites:
- Websites can be accessed on desktop computers, laptops, or mobile phones.
- Websites adapt their layout and functionality to the specific device being used.
- This is an example of runtime polymorphism as the website adjusts its behavior based on the device at runtime. The website code remains the same, but its behavior changes according to the device it's being accessed from.
-
Runtime Polymorphism in Person Roles:
- A person can play multiple roles, such as a parent, child, employee, and friend.
- Despite having various roles, a person is still a single entity.
- This is not an example of runtime polymorphism. Instead, it reflects the versatility of a person to perform different roles but remains the same entity.
-
Runtime Polymorphism in Computer Programs:
- A computer program, like a sorting algorithm, can be used to sort various data types (numbers, letters, objects).
- The sorting program's functionality remains consistent, but it can handle different data types.
- This is an example of runtime polymorphism because the sorting program's behavior adapts at runtime to sort different types of data, even though the core program logic stays the same.