Polymorphism in Java
A Simple Thought
Think about how you use the word “play.”
You can play a game, play a guitar, or play a video, and the meaning changes depending on the situation.
This is what polymorphism means in programming — the same word or method can behave differently depending on the context.
In Java, polymorphism allows one method to take on many forms.
It helps you write flexible and reusable code that can adapt to different objects.
The Project: Shape Drawer
You will create a program that can draw different shapes, such as circles and rectangles.
Each shape will have its own version of a method called draw(), and the program will call it using a single reference.
Your output should look like this:
1 2 3 4 5======================== SHAPE DRAWER Drawing a Circle Drawing a Rectangle ========================
Even though the same method name is used, each shape’s version works differently.
The Concept: Polymorphism
Polymorphism means “many forms.” In Java, it happens when one method name can perform different actions depending on the object calling it.
Here’s the basic structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17class Shape { void draw() { System.out.println("Drawing a Shape"); } } class Circle extends Shape { void draw() { System.out.println("Drawing a Circle"); } } class Rectangle extends Shape { void draw() { System.out.println("Drawing a Rectangle"); } }
Now, if you use a Shape reference to call draw(), Java will automatically use the correct version depending on the object.
Why This Matters
Polymorphism is one of the most powerful ideas in object-oriented programming.
It helps you:
- Write flexible programs that can handle new types easily
- Reduce repetition by reusing method names
- Improve readability and organization
In real-world applications, polymorphism allows systems to process many different types of data with one consistent interface, such as displaying different file types or handling various user actions.
Let’s Build the Shape Drawer Program
Here’s the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32class Shape { void draw() { System.out.println(" Drawing a Shape"); } } class Circle extends Shape { void draw() { System.out.println(" Drawing a Circle"); } } class Rectangle extends Shape { void draw() { System.out.println(" Drawing a Rectangle"); } } public class ShapeDrawer { public static void main(String[] args) { System.out.println("========================"); System.out.println(" SHAPE DRAWER"); Shape shape1 = new Circle(); Shape shape2 = new Rectangle(); shape1.draw(); shape2.draw(); System.out.println("========================"); } }
When you run this program, both objects share the same method name, draw(), but the output changes depending on the actual type of the object.
How It Works
- The Shape class defines a method called draw().
- The Circle and Rectangle classes both inherit from Shape and provide their own versions of draw().
- In the main program, both objects are stored in Shape type variables.
- When shape1.draw() is called, Java checks the actual type of the object and runs the correct version.
- This is called runtime polymorphism, the decision happens while the program is running.
Polymorphism lets your program handle different classes through one common interface.
Learn Together with AI
If you are using an AI Copilot, you can experiment with polymorphism by describing examples that show how one method can behave in multiple ways.
Try this prompt:
“Write a Java program that shows polymorphism using a Shape class with Circle and Rectangle subclasses that override a draw() method.”
Then try adding challenges like:
- “Add a Triangle class and override the draw() method.”
- “Use an array of Shape objects and loop through them to draw each one.”
- “Add a method that prints the name of each shape automatically.”
These experiments will help you see how polymorphism can make your code dynamic and extendable.
Practice Challenge
Create a program that uses polymorphism to represent animals.
There should be a base class called Animal and subclasses Dog and Cat.
Each subclass should override a method called sound().
Your output should look like this:
1 2 3 4 5======================== ANIMAL SOUNDS Dog says Woof Cat says Meow ========================
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32class Animal { void sound() { System.out.println(" Animal makes a sound"); } } class Dog extends Animal { void sound() { System.out.println(" Dog says Woof"); } } class Cat extends Animal { void sound() { System.out.println(" Cat says Meow"); } } public class AnimalSounds { public static void main(String[] args) { System.out.println("========================"); System.out.println(" ANIMAL SOUNDS"); Animal a1 = new Dog(); Animal a2 = new Cat(); a1.sound(); a2.sound(); System.out.println("========================"); } }
When you run this program, you’ll see how the same method, sound(), works differently for each object type.
This is polymorphism in action — one method, many behaviors.
Coming Up Next
You’ve learned how polymorphism allows a single method to perform different tasks depending on the object.