Stop 05 · Inheritance, Polymorphism & InterfacesTopics · 2/9

Method Overriding & Polymorphism

Part of the Java Roadmap

In short

Polymorphism is why one line like n.send(message) can send an SMS, an email or a WhatsApp alert. It lets you add new behaviour without touching the code that calls it.

Overriding lets a subclass replace a parent method. At runtime, Java calls the version that matches the actual object type, so a parent-type reference can behave differently per object.

The jaadu rings a big brass school bell in a courtyard while small children, older students and teachers each hurry off in different directions.

Think of it like this

The same school bell

When the bell rings at 1 pm, every class hears the same signal. The nursery kids line up for lunch, the Class 10 students rush to the lab, the teachers walk to the staff room. One message, many responses, each decided by who hears it. That's polymorphism: the same method call, different behaviour depending on the actual object.

Watch it happen

One loop, three different send() calls

SmsNotification
0
EmailNotification
1
WhatsAppNotification
2

1/4The array's type is Notification[], but each slot holds a different subclass object.

The big ideas

What you need to know

Overriding

A subclass writes a method with the same name, parameters and return type as the parent. Its version replaces the parent's for that object.

Always add @Override

The compiler then checks you really are overriding. A typo like sendd() becomes a compile error instead of a silent new method.

Decided at runtime

Notification n = new SmsNotification(); n.send() runs the SMS version. The reference type decides what you may call, the object decides which version runs.

Overloading is different

Overloading is same name, different parameters, chosen at compile time. Overriding is same signature in a subclass, chosen at runtime.

See it in code

Walk through the code

java
1class Notification {2    void send(String msg) {3        System.out.println("Notice: " + msg);4    }5}6 7class SmsNotification extends Notification {8    @Override9    void send(String msg) {10        System.out.println("SMS: " + msg);11    }12}13 14class EmailNotification extends Notification {15    @Override16    void send(String msg) {17        System.out.println("Email: " + msg);18    }19}20 21public class Main {22    public static void main(String[] args) {23        Notification[] channels = { new SmsNotification(), new EmailNotification(), new Notification() };24        for (Notification n : channels) {25            n.send("Your Mumbai order has shipped");26        }27    }28}

1/4The parent defines send() with a default behaviour.

Prints an SMS line, an email line and a plain notice line from the same loop.

Save yourself the pain

Common mistakes

Oops

Changing the parameter list and thinking you overrode the method.

Do this instead

send(String msg, int priority) is an overload, not an override. Add @Override and the compiler will tell you.

Oops

Expecting static methods or fields to behave polymorphically.

Do this instead

Only instance methods are overridden. Static methods and fields are picked by the reference type, not the object.

Oops

Making the overriding method less visible, like overriding a public method without public.

Do this instead

An override can keep or widen access, never narrow it. Narrowing is a compile error.

Your mission

Add a channel without editing the loop

~20 min

Quick check

Notification n = new SmsNotification(); n.send("Hi"); Which send() runs?

Go deeper

  1. 1Store different Shape subclasses in one array and call area() on each.
  2. 2Always add @Override so the compiler checks your method signature.
  3. 3Explain the difference between overloading and overriding.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge