• Articles
  • Tutorials
  • Interview Questions

Polymorphism in Java - Types, Examples, and Benefits

Define Polymorphism in Java

In the realm of object-oriented programming, the principle of polymorphism emerges as a cornerstone, offering a pathway for objects from distinct classes to be embraced as instances of a shared superclass. This paradigm embodies adaptability, reusability, and expansion in code design, resonating harmoniously with the principles of abstraction and encapsulation. Within Java, polymorphism manifests itself through the art of method overriding and method overloading, showcasing the language’s profound capacities for object-oriented programming.

Types of Polymorphism in Java

Compile-Time Polymorphism (Method Overloading): Method overloading is a form of static polymorphism where multiple methods in the same class have the same name but differ in the number or type of parameters. The appropriate method to be invoked is determined at compile-time based on the method’s signature.

Example:

class Calculator {

    int add(int a, int b) {

        return a + b;

    }

    double add(double a, double b) {

        return a + b;

    }

}

Run-Time Polymorphism (Method Overriding): Method overriding is a type of dynamic polymorphism where a subclass provides a specific implementation of a method that is already defined in its superclass. The method to be executed is determined at runtime based on the object’s actual type.

Example:

class Shape {

    void draw() {

        System.out.println("Drawing a shape");

    }

}

class Circle extends Shape {

    void draw() {

        System.out.println("Drawing a circle");

    }

}

Run-Time Polymorphism (Method Overriding): Runtime polymorphism is a characteristic within object-oriented programming that grants the ability for a single method name to possess distinct implementations across diverse classes. The determination of which specific method is invoked occurs during runtime, contingent upon the particular object’s type that is under consideration.

For example, consider the following code:

class Animal {

    public void speak() {

        System.out.println("I am an animal!");

    }

}

class Dog extends Animal {

    @Override

    public void speak() {

        System.out.println("Woof!");

    }

}

class Cat extends Animal {

    @Override

    public void speak() {

        System.out.println("Meow!");

    }

}

public class Main {

    public static void main(String[] args) {

        Animal animal = new Animal();

        animal.speak(); // Prints "I am an animal!"

        Dog dog = new Dog();

        dog.speak(); // Prints "Woof!"

        Cat cat = new Cat();

        cat.speak(); // Prints "Meow!"

    }

}

Polymorphism via Interfaces: Utilizing Java interfaces facilitates the attainment of polymorphism by delineating an agreement that multiple classes can adhere to. This empowers objects from assorted classes to be treated cohesively when they adopt a shared interface.

abstract class Shape {

    abstract void draw();

}

class Circle extends Shape {

    void draw() {

        System.out.println("Drawing a circle");

    }

}

class Square extends Shape {

    void draw() {

        System.out.println("Drawing a square");

    }

}

Benefits of Polymorphism in Java

  • Code Extensibility: Polymorphism allows developers to extend and expand upon existing classes and behaviors without affecting existing code. This is crucial for adapting to changing requirements.
  • Method Overriding: Inheritance and polymorphism work together to enable method overriding, where subclasses can provide their own implementations of methods defined in the superclass. This promotes customization and specialization.
  • Loose Coupling: Polymorphism promotes loose coupling between classes, making it easier to modify and maintain the codebase. Changes to one class are less likely to impact others.
  • Enhanced Collaboration: By allowing various objects to interact through a common interface, polymorphism encourages collaboration between different parts of a program or between different software components.

Java Polymorphism Examples

Let’s discuss Java Polymorphism Examples

  • Shape Drawing Application: Consider a drawing application that allows users to create various shapes like circles, squares, and triangles. Each shape class can implement a common interface Drawable with a method draw(). Users can interact with different shape objects uniformly, regardless of their specific type, due to polymorphism. This flexibility enables the application to accommodate new shapes easily without altering existing code.
interface Drawable {

    void draw();

}

class Circle implements Drawable {

    public void draw() {

        System.out.println("Drawing a circle");

    }

}

class Square implements Drawable {

    public void draw() {

        System.out.println("Drawing a square");

    }

}

public class DrawingApp {

    public static void main(String[] args) {

        Drawable circle = new Circle();

        Drawable square = new Square();

        

        circle.draw(); // Output: Drawing a circle

        square.draw(); // Output: Drawing a square

    }

}
  • Banking System: In a banking system, different account types (e.g., Savings Account, Checking Account) can inherit from a common superclass Account. Each account type can have its own implementations for methods like calculateInterest(). Despite the varying interest calculation methods, users can interact with different account objects using a standardized interface.
class Account {

    double balance;

    void calculateInterest() {

        // Common interest calculation logic

    }

}

class SavingsAccount extends Account {

    void calculateInterest() {

        // Specific interest calculation for savings account

    }

}

class CheckingAccount extends Account {

    void calculateInterest() {

        // Specific interest calculation for checking account

    }

}

public class BankingApp {

    public static void main(String[] args) {

        Account savings = new SavingsAccount();

        Account checking = new CheckingAccount();

        savings.calculateInterest(); // Calls SavingsAccount's calculateInterest

        checking.calculateInterest(); // Calls CheckingAccount's calculateInterest

    }

}
  • Animal Classification: Imagine a zoo management system where animals are categorized into classes like Mammals, Reptiles, and Birds. Each animal class can implement methods like makeSound() and move(). Regardless of the animal’s category, visitors can interact with them through a common interface, such as observing their movements or hearing their sounds.
interface Animal {

    void makeSound();

}

class Lion implements Animal {

    public void makeSound() {

        System.out.println("Roar!");

    }

}

class Parrot implements Animal {

    public void makeSound() {

        System.out.println("Squawk!");

    }

}

public class ZooApp {

    public static void main(String[] args) {

        Animal lion = new Lion();

        Animal parrot = new Parrot()

        lion.makeSound(); // Output: Roar!

        parrot.makeSound(); // Output: Squawk!

    }

}
  • Sorting Algorithms: In a sorting algorithm library, different sorting algorithms (e.g., Bubble Sort, Quick Sort) can implement a common interface SortAlgorithm. This enables developers to switch between sorting algorithms seamlessly without altering the sorting logic in the client code, showcasing polymorphism’s adaptability.
interface SortAlgorithm {

    void sort(int[] array);

}

class BubbleSort implements SortAlgorithm {

    public void sort(int[] array) {

        // Bubble sort implementation

    }

}

class QuickSort implements SortAlgorithm {

    public void sort(int[] array) {

        // Quick sort implementation

    }

}

public class SortingApp {

    public static void main(String[] args) {

        SortAlgorithm bubbleSort = new BubbleSort();

        SortAlgorithm quickSort = new QuickSort();   

        int[] array = {5, 2, 8, 1, 9};

        bubbleSort.sort(array); // Calls BubbleSort's sort method

        quickSort.sort(array); // Calls QuickSort's sort method

    }

}
  • Printers and Printing Documents: In a printing application, various printer types (e.g., Laser Printer, Inkjet Printer) can implement a shared interface Printer. Each printer type can have its own implementation of the print() method. Users can print documents without being concerned about the specific printer type they are using.
interface Printer {

    void print(String document);

}

class LaserPrinter implements Printer {

    public void print(String document) {

        System.out.println("Printing using a laser printer: " + document);

    }

}

class InkjetPrinter implements Printer {

    public void print(String document) {

        System.out.println("Printing using an inkjet printer: " + document);

    }

}

public class PrintingApp {

    public static void main(String[] args) {

        Printer laserPrinter = new LaserPrinter();

        Printer inkjetPrinter = new InkjetPrinter();

        

        laserPrinter.print("Report"); // Output: Printing using a laser printer: Report

        inkjetPrinter.print("Memo"); // Output: Printing using an inkjet printer: Memo

    }

}

These examples illustrate how polymorphism in Java allows objects of different classes to be treated uniformly, promoting code reusability, flexibility, and extensibility. By adhering to common interfaces or class hierarchies, Java programmers can build versatile applications capable of handling diverse data and behaviors without sacrificing cohesion.

Conclusion

Polymorphism in Java is a cornerstone of object-oriented programming, promoting flexibility, reusability, and adaptability in software design. It’s facilitated through mechanisms like method overloading, method overriding, and interface implementations. By embracing polymorphism, Java developers can create more modular, maintainable, and extensible applications that efficiently handle diverse data and behaviors.

Course Schedule

Name Date Details
Python Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.