Tutorials Logic, IN info@tutorialslogic.com

Polymorphism in Java Overloading Overriding

Compile-Time Polymorphism: Overloading

Overloading is selected from compile-time signatures, while overriding dispatches from the runtime object. Keeping those mechanisms separate makes polymorphic calls predictable.

A strong polymorphism example uses a parent reference such as Shape, Payment, or Notification and then calls the same method on different child objects. The result proves that runtime behavior depends on the actual object.

Overloading chooses a method at compile time based on the method name and parameter list.

Overloading

Overloading
class Printer {
    void print(String text) {
        System.out.println(text);
    }

    void print(int number) {
        System.out.println(number);
    }
}

Runtime Polymorphism: Overriding

Overriding chooses the method implementation at runtime based on the actual object type.

Dynamic Dispatch

Dynamic Dispatch
class Payment {
    void pay(double amount) {
        System.out.println("Generic payment: " + amount);
    }
}

class UpiPayment extends Payment {
    @Override
    void pay(double amount) {
        System.out.println("UPI paid: " + amount);
    }
}

public class PolymorphismDemo {
    public static void main(String[] args) {
        Payment payment = new UpiPayment();
        payment.pay(500);
    }
}

Polymorphism with Interfaces

Interfaces make polymorphism more flexible because unrelated classes can implement the same contract.

Interface Polymorphism

Interface Polymorphism
interface Notifier {
    void send(String message);
}

class EmailNotifier implements Notifier {
    public void send(String message) {
        System.out.println("Email: " + message);
    }
}

class SmsNotifier implements Notifier {
    public void send(String message) {
        System.out.println("SMS: " + message);
    }
}

Compile-Time and Runtime Polymorphism

Method overloading is resolved by the compiler using the method name and parameter list. Method overriding is resolved at runtime using the actual object type, which is why parent references can call child behavior.

  • Overloading means same name with different parameters.
  • Overriding means child class changes inherited behavior.
  • Runtime polymorphism supports flexible code.
  • Use @Override to catch mistakes.
Before you move on

Polymorphism in Java Overloading Overriding Mastery Check

4 checks
  • Separate overload selection at compile time from overridden method dispatch at runtime.
  • Call an overridden method through a base-type reference and predict the implementation selected.
  • Use @Override to catch signature mistakes, and do not describe hidden fields or static methods as dynamic dispatch.
  • Check that each subtype preserves the behavioral promises of its parent type.

Polymorphism Failure Boundary

  • Unsafe downcast

    A base reference does not guarantee one concrete subtype. Program to the shared contract and downcast only after an explicit type check when subtype behavior is unavoidable.

Core Java Questions Learners Ask

The actual object type, not the reference variable type.

No. The compiler selects an overload from the declared argument types.

It stores a subclass object in a superclass reference, preserving access to overridden behavior.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.