difference between static and dynamic polymorphism in java
Static polymorphism and dynamic polymorphism are two types of polymorphism in Java, each achieved through method overloading and method overriding, respectively. Let’s compare them in terms of their characteristics:
![static and dynamic polymorphism in java static and dynamic polymorphism in java](http://www.testpreparationz.com/wp-content/uploads/2024/01/Method-Overloading-in-Java.png)
Static Polymorphism (Compile-Time Polymorphism):
1. Definition: Static Polymorphism: Also known as compile-time polymorphism, it occurs when the method to be executed is determined at compile time.
2. Mechanism: Achieved through method overloading, where multiple methods in the same class have the same name but different parameter lists (different types or different numbers of parameters).
3. Decision Time: Decisions about which method to call are made by the compiler during the compilation phase.
4. Binding: Static Binding: The method call is resolved at compile time.
Example:
public class MathOperations {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
Usage:
MathOperations math = new MathOperations();
int result1 = math.add(2, 3); // Calls the first method
double result2 = math.add(2.5, 3.5); // Calls the second method
Dynamic Polymorphism (Run-Time Polymorphism):
1. Definition: Dynamic Polymorphism: Also known as run-time polymorphism, it occurs when the method to be executed is determined at runtime.
2. Mechanism: Achieved through method overriding, where a subclass provides a specific implementation for a method already defined in its superclass.
3. Decision Time: Decisions about which method to call are made at runtime based on the actual type of the object.
4. Binding: Dynamic Binding: The method call is resolved at runtime.
Example:
// Parent class
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
// Subclass overriding the makeSound method
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
Usage:
Animal myAnimal = new Dog();
myAnimal.makeSound(); // Calls the overridden method in Dog
Key Differences:
Feature | Static Polymorphism (Compile-Time Polymorphism) | Dynamic Polymorphism (Run-Time Polymorphism) |
---|---|---|
Mechanism | Achieved through method overloading (same class). | Achieved through method overriding (subclass). |
Decision Time | Decided at compile time. | Decided at runtime based on the actual type. |
Binding | Static binding. | Dynamic binding. |
Example | java public int add(int a, int b) { //... } public double add(double a, double b) { //... } | java class Animal { void makeSound() { //... } } class Dog extends Animal { @Override void makeSound() { //... } } |
Usage | Method overloading within the same class. | Method overriding between a superclass and its subclass. |
Flexibility | Limited flexibility as the decision is made at compile time. | Greater flexibility as the decision is made at runtime. |
In summary, static polymorphism involves compile-time decisions and is achieved through method overloading within the same class, while dynamic polymorphism involves runtime decisions and is achieved through method overriding in a subclass. Both types contribute to the concept of polymorphism in Java.