Understanding Method Overriding in Java with Example Code

Robert Jul 13, 2026

In the realm of Object-Oriented Programming, specifically in Java, overloading and overriding are fundamental concepts that enable dynamic behavior. Both involve defining methods with the same name but differ in how they function. Overloading, also known as polymorphism, allows multiple methods to have the same name but different parameters. On the other hand, overriding is a mechanism where a subclass provides a specific implementation of a method defined in its superclass. This article delves into the concept of overriding in Java, complete with examples.

Method Overriding in Java with Examples - Scientech Easy
Method Overriding in Java with Examples - Scientech Easy

Before we dive into overriding, let's remember that Java is a statically-typed language. It means that the type checks are performed at compile time. Overriding in Java is a feature that allows a subclass or a child class to provide a specific implementation of a method that is already defined in its parent class or superclass.

Example of Java Method Overriding
Example of Java Method Overriding

Understanding Method Overriding in Java

Method overriding in Java occurs when a subclass provides a specific implementation of a method that is already defined in its parent class. The method in the superclass is known as the override method, and the method in the subclass is the overriding method.

Method Overriding in Java - Explore the 10 Rules with Coding Examples
Method Overriding in Java - Explore the 10 Rules with Coding Examples

The overriding method must have the same return type, the same method name, and the same number and type of parameters as the override method in the superclass. If the return type of the overriding method is void, it can be overridden with a method that throws exceptions.

Java's Rule for Method Overriding

Main Difference Between Overloading And Overriding in Java
Main Difference Between Overloading And Overriding in Java

To ensure correct behavior, Java has some for overriding methods. The following rules must be adhered to:

  1. Both methods, the override and the overriding method, must have the same number and type of parameters.
  2. The returning type of the overriding method must be either identical or a co-variant (meaning it's a subclass) to that of the override method.
  3. The access modifier of the overriding method must be wider than or equal to the superclass's method. For instance, if a method is protected in the superclass, it can be overridden with either protected or public in the subclass.
  4. The overridden method must not throw a checked exception that is new or broader than the ones thrown by the method in the superclass.

Example of Method Overriding in Java

Method Overloading And Method Overriding In Java
Method Overloading And Method Overriding In Java

Let's look at a simple example of overriding in Java. Suppose we have a superclass Animal and a subclass Dog.

class Animal {
  void sound() {
    System.out.println("The animal makes a sound");
  }
}

class Dog extends Animal {
  @Override
  void sound() {
    System.out.println("The dog barks");
  }
}

The Dog class extends the Animal class, and we've overridden the sound method in the Dog class. When we execute the following code:

Animal animal = new Dog();
animal.sound(); // Prints: The dog barks

The output will be "The dog barks" because the sound() method in the Dog class is called. Even though we instantiated the Animal type, the overridden method in the Dog class is executed.

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

Importance of Method Overriding

Method overriding is a fundamental aspect of polymorphism. It allows us to use a common interface for general classes, but implement specialized behavior for subclasses. It enhances code reusability, makes object-oriented programming more efficient, and promotes flexible and adaptable software design.

Method Overloading vs Method Overriding in Java | OOP Concepts Easy Notes
Method Overloading vs Method Overriding in Java | OOP Concepts Easy Notes
What is Constructor Overloading in Java? Example
What is Constructor Overloading in Java? Example
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
Checkout Method Overriding in Java with Rules and Real-time Examples
Checkout Method Overriding in Java with Rules and Real-time Examples
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Difference between Method Overloading and Overriding in Java
Difference between Method Overloading and Overriding in Java
Can you Overload or Override main method in Java? Example
Can you Overload or Override main method in Java? Example
Can You Overload or Override Static methods in Java? Example
Can You Overload or Override Static methods in Java? Example
the cover of an ebook with text that reads, increment & decrement operators
the cover of an ebook with text that reads, increment & decrement operators
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
3 Ways to Prevent Method Overriding in Java - Private, Static and Final Method Example Tutorial
3 Ways to Prevent Method Overriding in Java - Private, Static and Final Method Example Tutorial
The OOP'S Concept in JAVA🤔💯✅👇
The OOP'S Concept in JAVA🤔💯✅👇
Constructor Overloading in Java | Example Program - Scientech Easy
Constructor Overloading in Java | Example Program - Scientech Easy
Explore Method Overloading vs Overriding in Java
Explore Method Overloading vs Overriding in Java
Why use @Override annotation in Java? Coding Best Practice Example
Why use @Override annotation in Java? Coding Best Practice Example
Java Method Overriding Explained with Animal Class | Java Tutorial
Java Method Overriding Explained with Animal Class | Java Tutorial
Method Overriding in Python (with Example) - Scientech Easy
Method Overriding in Python (with Example) - Scientech Easy
Best Java Training in Chennai|Java Course Chennai - Enroll Now
Best Java Training in Chennai|Java Course Chennai - Enroll Now
an image of a computer screen with the words if else statement on it and two blocks of code
an image of a computer screen with the words if else statement on it and two blocks of code
What is method overriding?
What is method overriding?

In conclusion, understanding and effectively utilizing method overriding in Java is a cornerstone for effective object-oriented design and programming. It's a crucial concept that empowers developers to create dynamic and efficient code.

Now that you've gained insights into Java's method overriding, why not explore more about this topic? Familiarize yourself with more advanced use-cases and engage in hands-on coding to truly grasp the power of overriding in Java.