Java Polymorphism
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Polymorphism

Harine

 Java Polymorphism

Polymorphism is a fundamental concept in object-oriented programming (OOP) that means “many forms.” In Java, polymorphism occurs when multiple classes related by inheritance define the same method differently. This allows a single interface (method call) to behave differently depending on the object type.

Polymorphism works hand-in-hand with inheritance: a superclass defines a common method,and subclasses provide their own implementations.

What Is Polymorphism in Java?

Polymorphism allows a single method name to represent different behaviors across relatedclasses.

In simple terms:

One method → different implementations → different results

Polymorphism Through Method Overriding

When a subclass provides its own version of a method defined in the superclass, it is called method overriding — the most common form of runtime polymorphism in Java.

Example: Shape Area Calculation (Polymorphism)

Instead of animals, let’s use a more realistic example: different shapes calculating area in different ways.

class Shape {

    public double area() {

     return 0; // default implementation

   }

}


class Circle extends Shape {

   private double radius;

   public Circle(double radius) {

     this.radius = radius;

}

   @Override

public double area() {

   return Math.PI * radius * radius;

}

}

class Rectangle extends Shape {

  private double width;

  private double height;

  public Rectangle(double width, double height) {

     this.width = width;

     this.height = height;

}

  @Override

  public double area() {

    return width * height;

  }

}

public class PolymorphismDemo {

   public static void main(String[] args) {

     Shape s1 = new Circle(3);

     Shape s2 = new Rectangle(4, 5);

     System.out.println("Circle area: " + s1.area());

     System.out.println("Rectangle area: " + s2.area());

   }

}

Output

Circle area: 28.274333882308138

Rectangle area: 20.0

Key idea:

Even though s1 and s2 are declared as Shape, Java calls the correct subclass method at runtime. This is runtime polymorphism (dynamic method dispatch).

Classic Polymorphism Example (Different Behaviors)

class Notification {

   public void send() {

   System.out.println("Sending generic notification");

  }

}


class EmailNotification extends Notification {

   @Override

   public void send() {

     System.out.println("Sending email notification");

   }

}


class SMSNotification extends Notification {

   @Override

   public void send() {

     System.out.println("Sending SMS notification");

   }

}


public class NotificationTest {

  public static void main(String[] args) {

    Notification n1 = new EmailNotification();

    Notification n2 = new SMSNotification();

    n1.send();

    n2.send();

  }

}

Output

Sending email notification

Sending SMS notification

How Polymorphism Works in Java

Polymorphism relies on three OOP mechanisms:

  • Inheritance — classes share a common superclass
  • Method overriding — subclasses redefine behavior
  • Dynamic binding — method call resolved at runtime

Why Use Polymorphism?

Polymorphism makes programs more flexible and scalable

Advantages

  • Write generic, reusable code
  • Easily extend functionality
  • Reduce conditional logic (if/else)
  • Support open/closed principle
  • Enable framework and API design

When to Use Inheritance and Polymorphism

Use them together when:

  • Multiple classes share common features
  • Behavior varies by type
  • You want extensible design
  • You want to program to interfaces/superclasses

Examples:

  • Payment methods (Card, UPI, Wallet)
  • Vehicles (Car, Bike, Truck)
  • UI components (Button, TextBox, Checkbox)
  • Notifications (Email, SMS, Push)

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !