Introduction

Hello, learner! In today's exciting chapter, we will unravel Polymorphism, a prominent feature of Object-Oriented Programming (OOP). Specifically, we will study its role in maintaining backward compatibility while introducing new features. Think of it as a software update that introduces new functions without breaking the older functionality — ingenious, isn't it?

Understanding Polymorphism

Polymorphism, a principle that derives from the Greek words "poly" (many) and "morphism" (forms), enables a variable or method to assume multiple roles — to embody various behaviors or functions determined by its data type or class.

Consider a class Bird with a method canFly(). If we create subclasses like Sparrow and Penguin, we can override the canFly() method for certain subclasses. This demonstrates polymorphism in action.

class Bird {  // Superclass
    String canFly() {
        return "Unknown";
    }
}

class Sparrow extends Bird {  // Subclass
    @Override
    String canFly() {
        return "Yes, I can fly!";
    }
}

class Penguin extends Bird {  // Subclass
    @Override
    String canFly() {
        return "No, I prefer swimming.";
    }
}

public class Main {
    public static void main(String[] args) {
        Sparrow sparrow = new Sparrow();
        Penguin penguin = new Penguin();
        System.out.println("Sparrow says: " + sparrow.canFly());  // Output: "Yes, I can fly!"
        System.out.println("Penguin says: " + penguin.canFly());  // Output: "No, I prefer swimming."
    }
}
Polymorphism for Backward Compatibility

When adding new features, which introduce new behaviors to some components, polymorphism ensures that the existing parts function as before, thereby retaining backward compatibility. In complex cases, we maintain an older version of the method in the superclass for legacy support while offering newer functionalities in subclasses.

Take, for instance, a MathOperations class with a multiply() method that accepts two parameters. To support the multiplication of three numbers, we design a subclass, ExtendedMathOperations, and include a new overloaded multiply() method in it, ensuring backward compatibility.

class MathOperations {  // Superclass
    int multiply(int a, int b) {
        return a * b;
    }
}

class ExtendedMathOperations extends MathOperations {  // Subclass
    @Override
    int multiply(int a, int b) {
        return a * b;
    }

    int multiply(int a, int b, int c) {
        return a * b * c;
    }
}

public class Main {
    public static void main(String[] args) {
        MathOperations mathOps = new MathOperations();
        ExtendedMathOperations extendedMathOps = new ExtendedMathOperations();
        System.out.println(mathOps.multiply(2, 3));  // Output: 6
        System.out.println(extendedMathOps.multiply(2, 3));  // Output: 6, keeping backward compatibility
        System.out.println(extendedMathOps.multiply(2, 3, 4));  // Output: 24
    }
}
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal