In object-oriented programming, polymorphism lets the same interface or operation exhibit different behaviours depending on the input given. The idea is similar to biological polymorphism, in which organisms of the same species can exist in different forms.
Polymorphism helps us to code to an interface and has multiple implementations. There are two types of polymorphism in OOP: Compile-time polymorphism and Runtime polymorphism. We implement Compile-time polymorphism using method overloading and the Runtime polymorphism using method overriding.
Compile-time polymorphism
In compile-time polymorphism, the compiler resolves the method calls during compilation. Since this process happens statically, it is also called static polymorphism and exhibits static/early binding. It is implemented via method overloading.
Method Overloading
In method overloading, we can define multiple methods with the same name in a class but with different parameter lists. The methods can differ in the number, types, or order of their parameters. Suppose we need to perform different types of addition like adding two integers or three integers.
int addTwo (int first , int second) {
//logic
}
int addThree (int first , int second , int third) {
//logic
}Instead of defining a separate method for each operation, we can use method overloading to define multiple versions of a method named addition().
int addition (int first , int second) {
//Implementation code
}
int addition (int first , int second , int third) {
//Implementation code
}Method overloading makes the code easier to understand than defining separate method names for each operation. Here, the compiler automatically selects the appropriate overloaded method based on the arguments supplied in the method call. So programmers do not need to explicitly choose a different method name for each variation of an operation.
Runtime Polymorphism
In this type of polymorphism, the Java Virtual Machine (JVM) decide which method implementation to execute at runtime. This is known as runtime polymorphism or dynamic polymorphism. Since the method call is resolved during program execution, it performs dynamic (late) binding. We implement runtime polymorphism using method overriding.
Method Overriding
Method overriding comes into the picture when we deal with inheritance. We override a method of the superclass to provide meaningful or custom implementation according to the subclass. The overriding method (present in the child class) and overridden method must have the same prototype.
Example
public class Base {
//...
public void ovrMethod (int a){
//implemention
}
}
public class Child extends base {
//...
@Override
public void ovrMethod (int a){
//modified implemention
}
}
//using the methods.
//ref is assigned Child instance
Base ref = new Child();
//Child ovrMethod is called
ref.ovrMethod(0);
//ref is assigned Base instance
ref = new Base();
//Base ovrMethod is called
ref.ovrMethod(0);Working of method overriding
Generally, we call overridden method with the base class reference by creating a reference of the base type and assigning it an instance of the child class. To understand more about runtime polymorphism, one needs to understand typecasting in java. Refer to this blog.
When the method ovrMethod() is invoked through the reference ref, the JVM decides which implementation to execute at runtime based on the actual type of the object referenced by ref. If ref refers to an instance of the child class, the overriding method in the child class is executed. Otherwise, if ref refers to an instance of the base class, the method implementation defined in the base class is executed.
Comparison between Compile-time and Runtime polymorphism
Compile-time and runtime polymorphism differ in how method calls are resolved, how they are implemented, and when the appropriate method implementation is selected.
- In compile-time polymorphism, the compiler resolves the method call during compilation (static binding). In runtime polymorphism, the JVM decides which method implementation to execute at runtime (dynamic binding).
- Compile-time polymorphism is implemented through method overloading, whereas runtime polymorphism is implemented through method overriding.
- Inheritance is not required for compile-time polymorphism because method overloading happen within a single class. But inheritance is required for runtime polymorphism through method overriding.
- In compile-time polymorphism, overloaded methods must have the same name but different parameter lists. In runtime polymorphism, the overriding method must have the same signature as the method in the superclass, although its implementation can differ.
- Static methods can be overloaded but cannot be overridden in Java. This is because static methods are associated with classes rather than objects. A subclass can declare a static method with the same signature as one in its superclass, but this is known as method hiding, not method overriding. So static methods participate in compile-time polymorphism but not runtime polymorphism.
- Compile-time polymorphism offer faster method dispatch because the method call is resolved during compilation. Runtime polymorphism has additional method-dispatch overhead because the appropriate implementation is decided at runtime.
Conclusion
In this blog, we first understood polymorphism and its importance. Further, we explored the types and techniques by which they are implemented in Java. Finally, we had a comparative study of the two types — compile-time and runtime polymorphism in almost every aspect.
Enjoy learning.
