The word “poly” means many and “morphs” means forms, So it means many forms. Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways.
There are some key difference between Runtime and Compile Time Polymorphism :
Feature | Compile-Time Polymorphism | Runtime Polymorphism |
1. Definition | Compile time Polymorphism in C++ determines which function or operation to call based on the number, types, and order of arguments. | The decision of which function to call is determined at runtime based on the actual object type rather than the reference or pointer type. |
2. Binding | Static binding, where the function calls are statically bound to their definitions. | Dynamic binding, where the function calls are dynamically bound to their definitions. |
3. Polymorphism Type | Compile-time Polymorphism, also known as static or early binding Polymorphism. | Runtime Polymorphism, also known as late binding or dynamic Polymorphism. |
4. Function Overloading | Multiple functions have the same name but different parameters defined. The compiler determines which function to call based on the number and types of arguments. | Not applicable. |
5. Operator | Overloading Operators can be overloaded to perform different operations based on the types of operands. | Not applicable. |
6. Inheritance | Not involved. | Involves inheritance, where a derived class overrides a function from its base class. |
7. Execution Speed | Faster execution rate, as the compiler determines which function to call at compile-time. | Comparatively slower execution rate, as the decision of which function to call is made at runtime. |
8. Code Reusability | High code reusability, as the same function name can be used with different parameters. | High code reusability, as the same function name can be used with different object types. |
9. Polymorphism In C++ | Compile-time Polymorphism is achieved by function overloading of operator overloading. | Runtime Polymorphism is achieved by function overriding. |
Pingback: Polymorphism in OOPs – RajCTU