Method Overriding in Java Example w3schools: A Comprehensive Guide

Robert Jul 13, 2026

Method overriding in Java is a powerful feature that allows a subclass to provide its own implementation of a method defined in its superclass, essentially overriding the superclass's method. This is particularly useful when the default behavior of the superclass method is not suitable for the subclass. Let's explore method overriding with examples inspired by w3schools tutorials.

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

Java fosters code reusability and promotes object-oriented programming through concepts like inheritance and polymorphism, with method overriding being a critical facet of the latter. By overriding a method, a subclass can modify its behavior while maintaining the same method signature, potentially enhancing performance or tailoring functionality.

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

Understanding Method Overriding

To grasp method overriding, let's first consider a simple example of a superclass and a subclass. Imagine we have a Animal superclass and a Dog subclass:

Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes

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"); } }

Overriding the makeSound() Method

Checkout Method Overriding in Java with Rules and Real-time Examples
Checkout Method Overriding in Java with Rules and Real-time Examples

The Dog class extends the Animal class and overrides the makeSound() method. When the makeSound() method is called on a Dog object, the subclass's implementation (i.e., it barks) will be executed, not the superclass's implementation.

Let's verify this with a simple program:

Animal myAnimal = new Animal(); Animal myDog = new Dog(); myAnimal.makeSound(); // Output: "The animal makes a sound" myDog.makeSound(); // Output: "The dog barks"

String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics

Method Overriding Rules

To ensure proper functioning, method overriding in Java adheres to certain rules. Key rules include:

  • The method in the subclass must have the same name, return type, and parameter list as the method in the superclass.
  • The access modifier of the subclass's method must be the same or broader than the superclass's method. For example, you can't change a superclass public method to private in the subclass.
  • The subclass cannot reduce the visibility of the method (e.g., from public to private or protected).
Java Best Practices for Method Overloading? Examples
Java Best Practices for Method Overloading? Examples

The compiler enforces these rules to maintain the integrity of method overriding.

Method Overriding vs. Method Overloading

What is method overriding?
What is method overriding?
Method overloading in Java
Method overloading in Java
30 Days of Java – Day 9 | Java Methods Explained for Beginners
30 Days of Java – Day 9 | Java Methods Explained for Beginners
Can You Overload or Override Static methods in Java? Example
Can You Overload or Override Static methods in Java? Example
a flow diagram with instructions for using the method
a flow diagram with instructions for using the method
Difference between Method and constructor in Java - Codeforcoding
Difference between Method and constructor in Java - Codeforcoding
30 Days of Java – Day 18 | Java Encapsulation Explained for Beginners
30 Days of Java – Day 18 | Java Encapsulation Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
What is method overriding?
What is method overriding?
Constructor Overloading in Java | Example Program - Scientech Easy
Constructor Overloading in Java | Example Program - Scientech Easy
🚀 The Ultimate Guide to Java Method Overloading 🎯
🚀 The Ultimate Guide to Java Method Overloading 🎯
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Encapsulation in Java Explained Simply | OOP Concept with Example 💻✨
Encapsulation in Java Explained Simply | OOP Concept with Example 💻✨
Best Java Training in Chennai|Java Course Chennai - Enroll Now
Best Java Training in Chennai|Java Course Chennai - Enroll Now
the quick and easy way to learn how to use loops
the quick and easy way to learn how to use loops
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
30 Days of Java – Day 4 | Java If-Else Statement Explained for Beginners
30 Days of Java – Day 4 | Java If-Else Statement Explained for Beginners
Difference between Method and Constructor in Java and OOP? Example
Difference between Method and Constructor in Java and OOP? Example

Before we delve deeper into method overriding, let's differentiate it from method overloading, another crucial concept in Java.

Method overloading, also known as compiler-time polymorphism, allows multiple methods to exist in the same class with different parameters. On the contrary, method overriding, or runtime polymorphism, lets a subclass provide a different implementation of a method inherited from its superclass.

Key Differences

Here's how method overriding and method overloading differ:

  • Method Overriding: Occurs between a superclass and its subclass. The subclass provides a different implementation of the same method as the superclass, maintaining the method signature.
  • Method Overloading: Occurs within the same class. Multiple methods can have the same name but different parameters, enabling the methods to behave differently depending on the arguments passed during invocation.

Now that you understand the basic differences between method overriding and method overloading, let's explore some advanced scenarios in method overriding.

Method Overriding with Object Class Methods

In Java, every class is a subclass of the Object class, which serves as the root of the class hierarchy. The Object class provides several methods that you can override to customize your class's behavior. These methods include equals(), hashCode(), toString(), and others. Let's demonstrate this with the equals() method:

public class Student { private String name; private int id; @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Student student = (Student) obj; return id == student.id && Objects.equals(name, student.name); } }

Here, the Student class overrides the equals() method from the Object class to compare two Student objects based on their name and id fields.

Method overriding is an essential aspect of Java programming, enabling developers to create reusable and highly customizable code. By mastering method overriding, you can design efficient and robust applications that leverage the full power of object-oriented programming.