Master Overriding Method Java Example Tutorial

Robert Jul 13, 2026

In object-oriented programming, particularly in Java, a common task is to redefine the behavior of a method in a subclass. This is achieved by using the concept of method overriding. Overriding a method allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is beneficial when you want to change the behavior of a method without altering the existing code.

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

Let's consider an example to understand method overriding in Java. Suppose we have a superclass named 'Animal' with a method 'makeSound()'. It's reasonable to expect that different animals make different sounds. So, we can create subclasses like 'Dog' and 'Cat', each overriding the 'makeSound()' method to provide their specific implementations.

Example of Java Method Overriding
Example of Java Method Overriding

Method Overriding in Java

Method overriding occurs when the subclass provides the same method signature as in the superclass. The method signature includes the method name and the number, type, and order of its parameters. The method's return type and access modifier must also be compatible. If all these conditions are met, the method in the subclass overrides the method in the superclass.

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

When you call the overridden method from an object of the subclass, the Java runtime calls the subclass's method instead of the superclass's method. This allows for custom behavior in the subclass without modifying the superclass's code. It's a key principle of code reuse and maintaining the 'open-closed' principle from SOLID design principles.

Key Rules for Method Overriding in Java

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

To ensure that your code works as expected, it's essential to follow these rules when overriding methods:

  • Method Signature Requirements: The method name, return type, and parameter list in the subclass must match exactly with the method in the superclass.
  • Access Modifier Restrictions: The overridden method in the subclass cannot have a more restrictive access modifier than the superclass's method. For instance, you cannot override a 'public' method from the superclass with a 'private' method in the subclass.
  • Final, Private, and Static Methods: 'final', 'private', and 'static' methods cannot be overridden.

Example of Overriding a Method in Java

Method Overloading vs Method Overriding in Java | OOP Concepts Easy Notes
Method Overloading vs Method Overriding in Java | OOP Concepts Easy Notes

The following Java example demonstrates method overriding. Here, we have a superclass 'Animal' and two subclasses 'Dog' and 'Cat'.

```java public class Animal { public void makeSound() { System.out.println("The animal makes a sound"); } } public class Dog extends Animal { @Override public void makeSound() { System.out.println("The dog barks"); } } public class Cat extends Animal { @Override public void makeSound() { System.out.println("The cat meows"); } } ```

When you create an object of either the 'Dog' or 'Cat' class and call the 'makeSound()' method, you hear the specific sound of that animal because each subclass overrides the 'makeSound()' method from the superclass 'Animal'.

This ability to have different implementations of the same method in different classes based on the type of the object is a powerful feature of object-oriented programming. It allows you to write code that is both reusable and adaptable, helping you to write code that is easier to read, easier to modify, and ultimately easier to maintain.

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

Overriding Methods and Polymorphism

Method overriding is casually related to another pivotal concept in object-oriented programming: polymorphism. Polymorphism allows an object to take on many forms. It's typically seen when a reference to a superclass variable points to a subclass object, and the type of behavior is determined at runtime.

Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Checkout Method Overriding in Java with Rules and Real-time Examples
Checkout Method Overriding in Java with Rules and Real-time Examples
Method overriding in Java explained with examples and best practices
Method overriding in Java explained with examples and best practices
Method Overriding in Python (with Example) - Scientech Easy
Method Overriding in Python (with Example) - Scientech Easy
Difference between Method Overloading and Overriding in Java
Difference between Method Overloading and Overriding in Java
Method Overriding in Java
Method Overriding in Java
Method Hiding in Java - Scientech Easy
Method Hiding in Java - Scientech Easy
30 Days of Java – Day 15 | Java Polymorphism Explained for Beginners
30 Days of Java – Day 15 | Java Polymorphism Explained for Beginners
Can you Overload or Override main method in Java? Example
Can you Overload or Override main method in Java? Example
What is Constructor Overloading in Java? Example
What is Constructor Overloading in Java? Example
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
Java Method Overriding Explained with Animal Class | Java Tutorial
Java Method Overriding Explained with Animal Class | Java Tutorial
Java Best Practices for Method Overloading? Examples
Java Best Practices for Method Overloading? Examples
What is method overriding?
What is method overriding?
Method Overriding in Java with Examples - Scientech Easy
Method Overriding in Java with Examples - Scientech Easy
Can You Overload or Override Static methods in Java? Example
Can You Overload or Override Static methods in Java? Example
Explore Method Overloading vs Overriding in Java
Explore Method Overloading vs Overriding in Java
What is Polymorphism in Java? Overriding or Overloading?
What is Polymorphism in Java? Overriding or Overloading?
Method overloading in Java
Method overloading in Java
String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics

When a method is called on such a variable, the appropriate method (either from the superclass or the actual object's class) is executed based on the dynamic type of the object. This is excellent for writing flexible and efficient code, as a single piece of code can handle different types of objects without knowing their exact types.

Polymorphism in Practice

Here's an example demonstrating polymorphism's use with method overriding:

```java public class Test { public static void main(String[] args) { Animal animal1 = new Animal(); Animal animal2 = new Dog(); Animal animal3 = new Cat(); animal1.makeSound(); animal2.makeSound(); animal3.makeSound(); } } ```

In this example, the 'animal1' variable is of type 'Animal', and its 'makeSound()' method is called. The 'animal2' and 'animal3' variables are of type 'Animal', but they reference 'Dog' and 'Cat' objects, respectively. Despite the 'Animal' type, the 'makeSound()' method is dynamically bound to the actual object's class at runtime, creating a beautiful example of polymorphism.

Method Overriding vs. Method Overloading

Before we wrap up, it's crucial to understand the difference between method overriding and method overloading. They sound similar, but they're not the same concept:

  • Method Overriding: Occurs when a subclass provides a specific implementation of a method that's already defined in its superclass, with the same method signature.
  • Method Overloading: Occurs when multiple methods exist within the same class with the same name but different method signatures (parameters). The method with the most specific method signature is called based on the arguments available.

Remember, method overriding is about runtime polymorphism, while method overloading is about compile-time polymorphism.

In conclusion, method overriding is a powerful feature in Java that allows you to redefine the behavior of a method in a subclass. It's essential for maintaining the flexibility and adaptability of your code, meeting the unique needs of each class while avoiding code duplication. It's widely applied in many scenarios, including when working with inherited classes, and it's worth mastering to leverage the full power of object-oriented programming.

The ease and grace of overriding methods make them a vital tool in your Java developer's toolbox, and their importance is underscored when you consider their role in achieving polymorphism. So, take the time to understand, practice, and apply these concepts in your projects — your code, and your career, will be better for it.