Java Polymorphism: Method Overriding and Dynamic Method Dispatch

Polymorphism (originating from Greek meaning "many forms") is the ability of an object or method to take on multiple operational behaviors depending on context. In Java, polymorphism is divided into two fundamental types:

  1. Compile-Time Polymorphism (Static Binding): Achieved through Method Overloading and operator behavior. Resolved during compilation by javac.
  2. Runtime Polymorphism (Dynamic Binding): Achieved through Method Overriding and Dynamic Method Dispatch. Resolved at runtime by the JVM.

Method Overriding Rules in Java

  • The method name and parameter signature must match the parent method identically.
  • The return type must be identical or a subtype (Covariant Return Type).
  • The access modifier cannot be more restrictive than the parent method (e.g. if parent is protected, child cannot make it private; it can be protected or public).
  • final, static, and private methods cannot be overridden.

Dynamic Method Dispatch (Upcasting in Action)

Dynamic Method Dispatch occurs when a superclass reference variable holds a subclass Heap object:

// Base Abstract Class
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a smooth Circle with radius calculation.");
    }
}

class Rectangle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a 4-cornered Rectangle.");
    }
}

public class DynamicDispatchDemo {
    public static void main(String[] args) {
        // UPCASTING: Superclass reference holds subclass object
        Shape s1 = new Circle();
        Shape s2 = new Rectangle();

        // JVM determines which draw() to call at RUNTIME based on Heap object:
        s1.draw(); // Output: Drawing a smooth Circle...
        s2.draw(); // Output: Drawing a 4-cornered Rectangle.
    }
}

University Exam Corner (BCA / B.Sc CS)

Frequent Exam Question: "Can variables be overridden in Java?"

Answer: No! In Java, variables are NEVER polymorphic. If a subclass declares a variable with the same name as a superclass variable, it merely shadows (hides) the parent variable. Field access is resolved at compile time based strictly on the reference type, NOT the runtime Heap object!

Next Step

Now let us explore Encapsulation, JavaBeans conventions, and data hiding. Proceed to: Java Encapsulation & Getters/Setters →

Frequently Asked Questions (FAQ)

Q1. What is Dynamic Method Dispatch in Java?
Dynamic Method Dispatch is the runtime mechanism by which a call to an overridden method is resolved dynamically at execution time based on the actual object type present in the Heap, rather than the compile-time type of the reference variable.
Q2. Can static methods or private methods be overridden in Java?
No! Static methods belong to the class and are resolved at compile-time (method hiding, not overriding). Private methods are invisible to subclasses and therefore cannot be overridden.
Q3. What is the purpose of the @Override annotation?
The @Override annotation instructs the compiler to verify that the annotated method actually overrides a matching method in a superclass or interface. If there is a typo in the method name or parameter list, the compiler throws an immediate error.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Method Overloading →

Compile-time polymorphism counterpart.

Inheritance Hierarchy →

Superclass and subclass relationships.

Java Interfaces →

Interface-based polymorphism.

Abstraction in Java →

Abstract methods and contracts.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!