In Java, when we call a method inside a program, the method call is resolved either at the compile time or during runtime, which is known as static and dynamic dispatch respectively.
Static Dispatch (Compile-Time Polymorphism)
- When a method call is resolved at compile time, it is known as static dispatch. It is also referred as compile time polymorphism.
- There can be more than one methods with the same name in the class, however these methods have different parameters list. This concept is known as method overloading. In this case also, the method call is resolved at compile time.
Static Dispatch Example
class StaticDispatchExample {
void print(int a) {
System.out.println("Number: " + a);
}
void print(String str) {
System.out.println("String: " + str);
}
public static void main(String[] args) {
StaticDispatchExample example = new StaticDispatchExample();
example.print(10); // Calls print(int a)
example.print("Hello"); // Calls print(String str)
}
}
In this example, the method call example.print(10)
and example.print("Hello")
are resolved at compile time based on the parameters list. This is because compile is smart enough to judge which method is being called based on the arguments that we passed during method call.
Dynamic Dispatch (Run-Time Polymorphism)
- When a method call is resolved during runtime, it is known as dynamic dispatch. It is also referred as run time polymorphism.
- The method call during method overriding is resolved during runtime, this is because the same method is present in both parent and child class and their parameters list is also same. In this case, compiler is not able to resolve the method call during compile time, thus this is resolved during runtime.
Dynamic Dispatch Example
class Parent {
void show() {
System.out.println("Parent show()");
}
}
class Child extends Parent {
@Override
void show() {
System.out.println("Child show()");
}
}
public class DynamicDispatchExample {
public static void main(String[] args) {
Parent obj = new Child();
obj.show(); // Calls Child's show() method at runtime
}
}
In this example, the show()
method in the child class is being called because the object (new child()
) belongs to child class.
Key Differences between static and dynamic dispatch
- Method call resolved at:
- Static Dispatch: Resolved at compile time.
- Dynamic Dispatch: Resolved at runtime.
- Polymorphism Type:
- Static Dispatch: Compile-time polymorphism (Method Overloading).
- Dynamic Dispatch: Run-time polymorphism (Method Overriding).
- Flexibility:
- Static Dispatch: Less flexible, as decisions are made at compile time.
- Dynamic Dispatch: More flexible, as decisions are made at runtime.
- Example:
- Static Dispatch: Method overloading.
- Dynamic Dispatch: Method overriding.
Can a developer decide which dispatch to use?
A developer designs the class and methods so they can write the code in such a way, that a particular dispatch is being used. However, they cannot directly choose dispatch mechanism during method calls, as this is determined by the rules of the programming language. If a developer want to influence which dispatch to use, they can do by using following approach:
- In order to use static dispatch, a developer can use the concept of method overloading in the program and avoid writing the same method (with same parameter list) in parent and child class both.
- In order to use dynamic dispatch, a developer can use the concept of method overriding.
Similar articles:
Leave a Reply