difference between compile time and run time polymorphism in java

What is difference between compile time and run time polymorphism in java

Compile-time polymorphism and run-time polymorphism are two forms of polymorphism in Java. Let’s explore the differences between them:

Compile-Time Polymorphism (Method Overloading):

1. Definition: Compile-Time Polymorphism: Also known as static polymorphism or method overloading, it occurs when multiple methods in the same class have the same name but different parameter lists (different types or different numbers of parameters).

2. Resolution Time: Compile-Time Polymorphism: Decisions about which method to call are made by the compiler during the compilation phase.

Example:

Example
public class MathOperations {
    // Method to add two integers
    public int add(int a, int b) {
        return a + b;
    }

    // Method overloading to add three integers
    public int add(int a, int b, int c) {
        return a + b + c;
    }
}

Usage:

Example

MathOperations math = new MathOperations();
int result1 = math.add(2, 3);         // Calls the first method
int result2 = math.add(2, 3, 4);      // Calls the second method

Run-Time Polymorphism (Method Overriding):

1. Definition: Run-Time Polymorphism: Also known as dynamic polymorphism or method overriding, it occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.

2. Resolution Time: Run-Time Polymorphism: Decisions about which method to call are made at runtime based on the actual type of the object.

Example:

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:

Example

Animal myAnimal;

// Polymorphic behavior based on the actual type of the object
myAnimal = new Dog();
myAnimal.makeSound(); // Calls the overridden method in Dog

Key Differences:

  • Time of Decision:
    • Compile-Time Polymorphism: Decisions are made by the compiler during the compilation phase.
    • Run-Time Polymorphism: Decisions are made at runtime based on the actual type of the object.
  • Mechanism:
    • Compile-Time Polymorphism: Achieved through method overloading (static binding).
    • Run-Time Polymorphism: Achieved through method overriding (dynamic binding).
  • Example:
    • Compile-Time Polymorphism: Multiple methods with the same name but different parameter lists.
    • Run-Time Polymorphism: Subclass providing a specific implementation for a method in the superclass.

In summary, compile-time polymorphism (method overloading) is resolved at compile time based on the method signature, while run-time polymorphism (method overriding) is resolved at runtime based on the actual type of the object. Both forms of polymorphism contribute to the flexibility and versatility of object-oriented programming in Java.

FeatureCompile-Time Polymorphism (Method Overloading)Run-Time Polymorphism (Method Overriding)
DefinitionMultiple methods with the same name but different parameter lists in the same class.A subclass provides a specific implementation for a method already defined in its superclass.
Resolution TimeDecisions made by the compiler during compilation.Decisions made at runtime based on the actual type of the object.
MechanismAchieved through method overloading (static binding).Achieved through method overriding (dynamic binding).
Examplejava public int add(int a, int b) { //... } public int add(int a, int b, int c) { //... }java class Animal { void makeSound() { //... } } class Dog extends Animal { @Override void makeSound() { //... } }
Usagejava MathOperations math = new MathOperations(); int result1 = math.add(2, 3); int result2 = math.add(2, 3, 4);java Animal myAnimal; myAnimal = new Dog(); myAnimal.makeSound();
Time of DecisionDecided by the compiler during compilation.Decided at runtime based on the object’s actual type.
BindingStatic binding.Dynamic binding.

Leave a Reply

Your email address will not be published. Required fields are marked *