Module 4 of 6 · Java OOP Fundamentals · Beginner

Polymorphism: One Interface, Many Forms

Duration: 5 min

What is Polymorphism?

Polymorphism means 'many forms'. In Java, it allows objects of different classes to be treated as objects of a common parent class. It also allows you to write code that can work with objects of multiple types.

There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding).

Compile-time Polymorphism: Method Overloading

Method overloading occurs when a class has multiple methods with the same name but different parameters. The compiler determines which method to call based on the arguments provided.

public class Calculator {
    // Method 1: Add two integers
    public int add(int a, int b) {
        return a + b;
    }
    
    // Method 2: Add three integers
    public int add(int a, int b, int c) {
        return a + b + c;
    }
    
    // Method 3: Add two doubles
    public double add(double a, double b) {
        return a + b;
    }
    
    // Method 4: Add integer and double
    public double add(int a, double b) {
        return a + b;
    }
}
No output - this is a class definition
public class CalculatorDemo {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        
        System.out.println("Add two ints: " + calc.add(5, 10));
        System.out.println("Add three ints: " + calc.add(5, 10, 15));
        System.out.println("Add two doubles: " + calc.add(5.5, 10.5));
        System.out.println("Add int and double: " + calc.add(5, 10.5));
    }
}
Add two ints: 15
Add three ints: 30
Add two doubles: 16.0
Add int and double: 15.5

Runtime Polymorphism: Method Overriding

Runtime polymorphism occurs when a subclass overrides a method from the parent class. The actual method called is determined at runtime based on the object type, not the reference type.

public class Shape {
    public void draw() {
        System.out.println("Drawing a shape");
    }
}

class Circle extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

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

class Triangle extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a triangle");
    }
}
No output - this is a class definition
public class PolymorphismDemo {
    public static void main(String[] args) {
        // Create an array of Shape references
        Shape[] shapes = new Shape[3];
        shapes[0] = new Circle();
        shapes[1] = new Rectangle();
        shapes[2] = new Triangle();
        
        // Call draw() on each shape
        // The actual method called depends on the object type
        for (Shape shape : shapes) {
            shape.draw();
        }
    }
}
Drawing a circle
Drawing a rectangle
Drawing a triangle

Benefits of Polymorphism

• Code Reusability: Write generic code that works with multiple types
• Flexibility: Easy to add new types without changing existing code
• Maintainability: Cleaner, more organized code structure
• Extensibility: Easy to extend functionality through inheritance

💡 Tip: Polymorphism is one of the most powerful features of OOP. It allows you to write flexible, extensible code that can handle different types of objects.

Learn more: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

❓ What is the difference between compile-time and runtime polymorphism?

❓ What is a best practice when working with Polymorphism?

💡 Tip: Pro Tip: Master Polymorphism thoroughly. This foundation is crucial for writing professional Java code.

← Previous Continue interactively → Next →

Related Courses