Skip to content

Polymorphism in OOP: Definition, Java Example

Quick answer

Polymorphism in OOP defined through Oracle's Java tutorial, with a runnable Shape example, the overriding rule and the overloading confusion explained.

Polymorphism in OOP: Definition, Java Example

Polymorphism in OOP means that one name or interface can stand for different behaviour depending on the object behind it. In Java, subclasses define their own unique behaviours while sharing functionality of the parent class, and the right method is chosen at runtime based on the actual object, not the variable's declared type.

Key takeaways

  • Polymorphism lets a parent-type variable run a child's version of a method.
  • In Java, the JVM picks the method from the object, not from the variable's type.
  • Overriding needs the same signature and return type (or a subtype) in the subclass.
  • Overloading, same name with different parameters, is a separate idea that interviews often mix up with it.

On this page:

TermRuleDecided
OverridingSubclass method with the same signature and return type as the superclass methodAt runtime, by the object
OverloadingSame name, different parameters, in one classAt compile time, by the arguments
InheritanceA subclass reuses a parent classMakes overriding possible
EncapsulationHiding data behind methodsA different OOP pillar

What does polymorphism mean?

The Oracle Java Tutorials say subclasses of a class can define their own unique behaviours and yet share some of the same functionality of the parent class. The word itself means many forms: one call, different results.

A real-life picture: the payment button in an app works the same way for UPI, cards and wallets, while each payment method runs its own code. This is illustrative. The definition above comes from Oracle's Java tutorial, so the details below are Java-specific.

How does polymorphism work?

Oracle explains that the Java virtual machine calls the appropriate method for the object that is referred to in each variable, and does not call the method defined by the variable's type. For overriding, the Oracle overriding page says an instance method in a subclass with the same signature (name, plus the number and type of parameters) and return type as one in the superclass overrides it.

Compile this as Main.java:

class Shape {
    double area() {
        return 0;
    }
}

class Circle extends Shape {
    double r;

    Circle(double r) {
        this.r = r;
    }

    @Override
    double area() {
        return Math.PI * r * r;
    }
}

class Square extends Shape {
    double side;

    Square(double side) {
        this.side = side;
    }

    @Override
    double area() {
        return side * side;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape[] shapes = { new Circle(2), new Square(3) };
        for (Shape s : shapes) {
            System.out.printf("%.2f%n", s.area());
        }
    }
}

The output is 12.57, then 9.00. The loop variable has type Shape, yet Java runs Circle's area() for the first object and Square's for the second. Adding a Triangle class later needs no change to the loop.

Where is polymorphism used?

It appears wherever code treats different things through one common type: shapes, payment methods, notification channels, report exporters. Indian service-company and product-company interviews for Java roles often ask about it. Practise with Java interview questions and OOPs interview questions and answers. If Java is new to you, start with an introduction to Java programming.

What are the common mistakes and confusions?

  • Mixing up overloading and overriding. Overloading changes the parameters; overriding keeps the signature in a subclass. Oracle's page covers overriding, and many tutorials also call overloading compile-time polymorphism, which is a convention, not something Oracle's page says.
  • Expecting the variable's type to decide. A Shape variable holding a Circle still calls Circle's method.
  • Changing the signature by accident. A different parameter type creates an overload, not an override. The @Override annotation makes the compiler catch this.
  • Returning an unrelated type. Oracle notes an overriding method may return a subtype of the original, called a covariant return type.

Inheritance, abstraction, interfaces and encapsulation round out the OOP set. Read what is encapsulation in OOP next. For long-term reading, see 10 books every programmer must read.

Next step

Run the Shape program, then add a Triangle class and a new array entry without touching the loop. Confirm the loop still works. That small change is the practical benefit of polymorphism.

Frequently asked questions

What is polymorphism in OOP?

Polymorphism lets one interface or name produce different behaviour depending on the object. In Java, subclasses define their own behaviours while sharing parent functionality, and the JVM calls the method of the actual object.

What are the types of polymorphism?

Tutorials commonly name two: runtime polymorphism through method overriding, and compile-time polymorphism through method overloading. Oracle's tutorial pages cover the overriding side.

What is method overriding?

Overriding is when a subclass defines an instance method with the same signature and return type as one in its superclass, so the subclass version is used for its objects.

What is the difference between overloading and overriding?

Overloading means the same method name with different parameters in one class. Overriding means a subclass redefines an inherited method with the same signature. Overriding is resolved at runtime.

Why is polymorphism useful in real projects?

It lets code work with a common type, such as Shape, while new subtypes are added without changing that code. This keeps programs easier to extend.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.