Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
One Employee variable e points at Arun, then Emma (a Manager), then Lucas (a Developer), and the same e.work() gives three answers. One loop over Employee[] replaces a growing if-else chain, and instanceof plus a cast reaches holdMeeting().
What you will learn
- Store a child object in a parent reference (upcasting)
- Explain dynamic dispatch: the object, not the variable, decides which method runs
- Replace an if-else type chain with one loop over a parent-typed array
- Use instanceof and a cast to reach a child's own method safely
Before you start
- Java method overriding and the Object class
Polymorphism means "many forms": one call takes a different form depending on the object. Think of a universal remote. There's one Play button, and the TV and the music system each play in their own way.
The problem: every new class adds a branch
Imagine a method that describes an employee by checking its type: if it's a Manager, say it leads people; otherwise say it works. Add a Developer class and you reopen the method for another branch. Add a Director, another branch. The chain grows with every class. With polymorphism the whole thing becomes one call, e.work(), and each object brings its own version. The classes are the Employee family from method overriding.
The same e, a different object
package org.studyeasy.s03l12;
public class Ex01_Upcasting {
public static void main(String[] args) {
Employee e = new Employee("Arun", 60000);
System.out.println(e.work()); // Employee's version
e = new Manager("Emma", 95000, 6); // upcast
System.out.println(e.work()); // Manager's version
e = new Developer("Lucas", 80000, "Java");
System.out.println(e.work()); // Developer's version
}
}
Arun is working
Emma is working and leads 6 people
Lucas is writing Java
Putting a Manager object into an Employee variable is upcasting. It's automatic and safe, because a Manager is an Employee. The other way round isn't: Manager m = new Employee("Arun", 60000); is "incompatible types". Same variable, same line, three answers.
The type of e never changes. The object it points at decides which work() runs, and that's decided while the program runs. This is called dynamic dispatch.
One loop, no type checks
package org.studyeasy.s03l12;
public class Ex02_StaffLoop {
public static void main(String[] args) {
Employee[] staff = {
new Employee("Arun", 60000),
new Manager("Emma", 95000, 6),
new Developer("Lucas", 80000, "Java"),
new Director("Priya", 150000, 40, "Sales")
};
for (Employee e : staff) { // one loop, no if-chain
System.out.println(e.work());
}
}
}
Arun is working
Emma is working and leads 6 people
Lucas is writing Java
Priya is working and leads 40 people
One Employee array holds four kinds of object; they all fit because each one is an Employee. One call, four answers, no if. Priya is a Director with no work() of her own, so Java climbs to Manager's version. Add another kind of employee to the array and the loop doesn't change at all. In a large application, that's a big deal.
instanceof first, then cast
Through an Employee variable you can only call methods Employee has. e.holdMeeting() is "cannot find symbol", even when the object really is a Manager. The variable's type limits what you may call.
package org.studyeasy.s03l12;
public class Ex03_Downcasting {
public static void main(String[] args) {
Employee[] staff = {
new Employee("Arun", 60000),
new Manager("Emma", 95000, 6),
new Developer("Lucas", 80000, "Java"),
new Director("Priya", 150000, 40, "Sales")
};
for (Employee e : staff) {
System.out.println(e.work());
if (e instanceof Manager) { // check first, then cast
Manager m = (Manager) e;
System.out.println(m.holdMeeting());
}
}
}
}
Arun is working
Emma is working and leads 6 people
Emma meets a team of 6
Lucas is writing Java
Priya is working and leads 40 people
Priya meets a team of 40
instanceof asks the object (not the variable) "are you a Manager?". Emma is, and so is Priya, because a Director is a Manager. Then (Manager) e is a downcast: it gives a second reference, m, of type Manager, pointing at the same object, and holdMeeting() opens up. Arun and Lucas skip the block. Modern Java shortens this check-and-cast into one step; see pattern matching for instanceof.
Recap
| Term | Meaning |
|---|---|
| Upcasting | child object in a parent reference; automatic and safe |
| Dynamic dispatch | the object's own method runs, decided at run time |
instanceof | asks which class the object really is |
| Downcasting | a cast to reach the child's own methods, only after the check |
The reference type decides what you may call; the object decides what runs.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Classes and Objects: One Blueprint, Many Cars
Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.
Java Constructors and the this Keyword
A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.
Java Instance Methods, Getters, Setters and toString
Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.
Java static: Fields, Methods and Blocks Shared by the Whole Class
A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.
