codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

33 / 65 topics
32Java Inheritance33Java Polymorphism34Java Inner Classes35Java Abstraction36Java Interface37Java Enum
Tutorials/Java Programming/Java Polymorphism
☕Java Programming

Java Polymorphism

Updated 2026-05-12
30 min read

Java Polymorphism

Polymorphism is one of the core concepts in object-oriented programming (OOP) that allows objects to be treated as instances of their parent class. It enables a single interface to represent different underlying forms (data types). In Java, polymorphism is primarily achieved through method overriding and method overloading. This tutorial will focus on method overriding and how it leads to polymorphic behavior.

Introduction

Inheritance is the foundation of polymorphism in Java. When a subclass overrides a method from its superclass, it can provide a specific implementation that differs from the superclass's method. This allows objects of different subclasses to be treated as objects of their common superclass, leading to more flexible and reusable code.

Polymorphism is crucial because it promotes code reusability and flexibility. It enables developers to write more generic and maintainable code by allowing methods to operate on objects of different classes through a single interface.

Method Overriding

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The method signature (name, return type, and parameters) must be exactly the same in both the superclass and the subclass.

Example 1: Basic Method Overriding

Java
1class Animal {
2 void makeSound() {
3 System.out.println("Some generic animal sound");
4 };
5}
6
7class Dog extends Animal {
8 @Override
9 void makeSound() {
10 System.out.println("Bark");
11 }
12}
13
14public class Main {
15 public static void main(String[] args) {
16 Animal myAnimal = new Animal();
17 Animal myDog = new Dog();
18
19 myAnimal.makeSound(); // Output: Some generic animal sound
20 myDog.makeSound(); // Output: Bark
21 }
22}
Output
Some generic animal sound
Bark

In this example, the Dog class overrides the makeSound method of the Animal class. When we call makeSound on an object of type Dog, it executes the overridden method in the Dog class.

Example 2: Polymorphic Behavior

Polymorphism allows us to use a superclass reference to refer to objects of its subclasses. This is particularly useful when dealing with collections of objects.

Java
1class Animal {
2 void makeSound() {
3 System.out.println("Some generic animal sound");
4 }
5}
6
7class Dog extends Animal {
8 @Override
9 void makeSound() {
10 System.out.println("Bark");
11 }
12}
13
14class Cat extends Animal {
15 @Override
16 void makeSound() {
17 System.out.println("Meow");
18 }
19}
20
21public class Main {
22 public static void main(String[] args) {
23 Animal[] animals = {new Animal(), new Dog(), new Cat()};
24
25 for (Animal animal : animals) {
26 animal.makeSound();
27 }
28 }
29}
Output
Some generic animal sound
Bark
Meow

In this example, we have an array of Animal objects that includes instances of Animal, Dog, and Cat. When we iterate over the array and call makeSound on each object, Java determines at runtime which method to execute based on the actual object type.

Why Use Polymorphism?

  1. Code Reusability: Polymorphism allows you to write more generic code that can handle objects of different classes.
  2. Flexibility: It enables you to add new subclasses without modifying existing code, adhering to the Open/Closed Principle.
  3. Maintainability: Changes in one part of the system (e.g., a method implementation) do not affect other parts that rely on the superclass interface.

Practical Example

Let's create a practical example where we use polymorphism to manage different types of shapes.

Java
1abstract class Shape {
2 abstract double area();
3 abstract double perimeter();
4
5 void displayInfo() {
6 System.out.println("Area: " + area());
7 System.out.println("Perimeter: " + perimeter());
8 }
9}
10
11class Circle extends Shape {
12 private double radius;
13
14 public Circle(double radius) {
15 this.radius = radius;
16 }
17
18 @Override
19 double area() {
20 return Math.PI * radius * radius;
21 }
22
23 @Override
24 double perimeter() {
25 return 2 * Math.PI * radius;
26 }
27}
28
29class Rectangle extends Shape {
30 private double width;
31 private double height;
32
33 public Rectangle(double width, double height) {
34 this.width = width;
35 this.height = height;
36 }
37
38 @Override
39 double area() {
40 return width * height;
41 }
42
43 @Override
44 double perimeter() {
45 return 2 * (width + height);
46 }
47}
48
49public class Main {
50 public static void main(String[] args) {
51 Shape[] shapes = {new Circle(5), new Rectangle(4, 6)};
52
53 for (Shape shape : shapes) {
54 shape.displayInfo();
55 System.out.println();
56 }
57 }
58}
Output
Area: 78.53981633974483
Perimeter: 31.41592653589793

Area: 24.0
Perimeter: 20.0

In this example, we have an abstract class Shape with two subclasses Circle and Rectangle. Each subclass provides its own implementation of the area and perimeter methods. The Main class demonstrates how polymorphism allows us to treat different shapes as Shape objects and call their methods.

Summary

  • Polymorphism allows objects to be treated as instances of their parent class.
  • Method overriding is a key mechanism for achieving polymorphism in Java.
  • Polymorphism promotes code reusability, flexibility, and maintainability.

What's Next?

In the next topic, we will explore Java Inner Classes, which allow you to define a class within another class. This can be useful for creating more organized and modular code. Stay tuned!


PreviousJava InheritanceNext Java Inner Classes

Recommended Gear

Java InheritanceJava Inner Classes