You are currently viewing Override Methods and Use Polymorphism in Java

Override Methods and Use Polymorphism in Java

1. Introduction

Object-oriented programming (OOP) in Java promotes modular, maintainable, and reusable code. Among its core principles, polymorphism stands out as a key concept that empowers developers to write flexible and scalable systems. In this article, we will explain how method overriding works and how it enables polymorphism in Java.

As the Oracle documentation puts it,

“Polymorphism enables you to invoke derived class methods through a base class reference during runtime.”

This makes it possible to design programs that are more generic and extensible.

This article assumes a basic understanding of Java inheritance and will guide you through method overriding, runtime polymorphism, and practical design strategies.

2. What Is Method Overriding?

Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the one in the parent class.

Why Override Methods?

  • To customize behavior for a specific subclass
  • To implement runtime polymorphism
  • To support extensibility and testability in software design

Example: Basic Method Overriding

class Animal {
    public void speak() {
        System.out.println("Animal speaks");
    }
}

class Dog extends Animal {
    @Override
    public void speak() {
        System.out.println("Dog barks");
    }
}

In this example, the Dog class overrides the speak() method to provide its own behavior.

3. Understanding Polymorphism

Polymorphism means “many forms,” and in Java, it primarily refers to the ability of one interface to be used for different underlying types. Java supports runtime polymorphism through method overriding and dynamic method dispatch.

Example: Polymorphism in Action

Animal animal = new Dog();
animal.speak(); // Output: Dog barks

Although the reference type is Animal, the actual method that is executed is from the Dog class. This behavior is resolved at runtime, enabling flexible program design.

4. The Role of the @Override Annotation

Using the @Override annotation when overriding methods is a best practice. It ensures that the method indeed overrides a method from the superclass. If the method signature is incorrect or the superclass method doesn’t exist, the compiler throws an error.

@Override
public void speak() {
    System.out.println("Dog barks");
}

This small annotation prevents hard-to-find bugs and improves code readability.

5. Real-World Application of Polymorphism

Let’s imagine a payment system where different payment types (CreditCard, PayPal, etc.) need to process payments differently. Instead of writing conditional statements, you can take advantage of polymorphism.

public class PaymentProcessor {
    public void process(Payment payment) {
        payment.pay();
    }
}

Now, each subclass like CreditCard or PayPal will override the pay() method, and the processor will call the correct version automatically.

    // Base payment class
    class Payment {
        public void pay() {
            System.out.println("Processing generic payment");
        }
    }

    // CreditCard subclass
    class CreditCard extends Payment {
        @Override
        public void pay() {
            System.out.println("Processing credit card payment");
        }
    }

    // PayPal subclass
    class PayPal extends Payment {
        @Override
        public void pay() {
            System.out.println("Processing PayPal payment");
        }
    }

This approach follows the Open/Closed Principle — “software entities should be open for extension, but closed for modification.”

6. Benefits of Using Polymorphism

  • Code reusability: Write less duplicated logic by abstracting common behavior.
  • Scalability: Add new behaviors without modifying existing code.
  • Maintainability: Encapsulate variations within specific classes.
  • Testing: Use mock or stub implementations easily.

These benefits make polymorphism a cornerstone in clean architecture and domain-driven design.

7. Conclusion

Understanding how to override methods and use polymorphism is essential for writing professional-grade Java applications. It promotes flexibility, decouples components, and aligns with object-oriented principles.
As you continue your journey, explore our articles on Java interfaces and abstract classes to deepen your understanding of inheritance and polymorphic behavior.

You can find the complete code of this article here on GitHub.

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.