Welcome! In our previous lesson, we established how inheritance allows a subclass to inherit fields and methods from a superclass using the extends keyword. This is a fundamental technique for code reuse. But what happens when the inherited behavior isn't quite right for the subclass?
In this lesson, we will address that exact question by exploring method overriding. You will learn how a subclass can provide its own specific implementation for an inherited method. We will also clarify the crucial distinction between method overriding and method overloading, a topic that is not only a common point of confusion for new Java developers but also a frequent subject in technical interviews. Mastering this distinction is a key step toward writing clean, polymorphic code.
1. What is Method Overriding?
In the last lesson, you saw that when a Dog class extends an Animal class, it inherits the eat() method. But it's logical to assume a dog might eat differently than a generic animal. Method overriding allows the Dog class to provide its own version of the eat() method.
Method overriding occurs when a subclass (child class) has a method with the same name, parameters, and return type as a method in its superclass (parent class). The subclass's method "overrides" the superclass's method, providing a specialized implementation.
Let's watch a video that introduces this concept using a clear, practical example.
This video from Telusko provides an excellent introduction to method overriding. It starts by differentiating it from overloading and then uses a Calculator example to show why a subclass might need to change an inherited method's behavior.
Please watch the following segments: Introduction and core concept: This part explains why you would want to override a method and introduces the idea with a Class B overriding a method from Class A. Pay close attention to the phone analogy around the 4-minute mark, as it provides a great intuition for why a subclass's own method gets preference. Calculator example: Here, an AdvanceCalc class overrides the add method from its parent Calculator class to provide a different implementation. This is a tangible example of overriding in action.
As the video demonstrates, when you call an overridden method on a subclass object, it's the subclass's version of the method that executes. The AdvanceCalc object used its own add method, not the one from Calculator.
The @Override Annotation
When you override a method, it is a strong best practice to use the @Override annotation just above the method definition in the subclass.
class Animal {
public void makeSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("The dog barks");
}
}
This annotation serves two purposes:
- Readability: It clearly communicates to anyone reading the code that this method is intended to override a method from a superclass.
- Safety: It tells the compiler to check if you are actually overriding a method. If you make a mistake, like misspelling the method name or using different parameters (e.g.,
makeSounds()instead ofmakeSound()), the compiler will give you an error. Without the annotation, the compiler would just think you're creating a new method, leading to subtle bugs.
2. The Rules of Method Overriding
To correctly override a method, you must follow a specific set of rules. An article from Index.dev provides a very clear breakdown of these.
Overloading vs Overriding in Java: Differences, Examples ...
This article offers a deep dive into both overriding and overloading. We will focus on the rules for overriding first.
Please read the section titled "Rules for Method Overriding in Java". This list is a concise and accurate summary of the requirements.
Let's review the most important rules from that list:
- Same Method Signature: The method in the subclass must have the exact same name and parameter list (number, type, and order of parameters) as the method in the superclass.
- Inheritance is Required: Overriding can only occur in a subclass.
- Access Modifier Cannot Be More Restrictive: The access level of the overriding method cannot be more restrictive than the overridden method. For example, if the superclass method is
protected, the subclass method can beprotectedorpublic, but notprivate. This ensures that any code that could access the superclass method can also access the subclass method. - Return Type Must Be Covariant: The return type must be the same as the superclass method, or a subtype of it. This is called a covariant return type. For instance, if the superclass method returns
Number, the overriding method can returnIntegerorDouble(since they are subtypes ofNumber). final,static, andprivateMethods:- A method declared
finalcannot be overridden. Thefinalkeyword explicitly prevents subclasses from changing its behavior. - A method declared
staticcannot be overridden. Static methods belong to the class, not the object. If a subclass defines a static method with the same signature, it's a new method that "hides" the parent's, which is a different concept. - A method declared
privatecannot be overridden because it is not inherited by the subclass.
- A method declared
3. Overriding vs. Overloading: A Crucial Distinction
Now we come to the core of this lesson: distinguishing overriding from overloading. We covered method overloading in the first module, but it's essential to compare them directly.
- Overloading is about providing multiple methods in the same class with the same name but different parameters. It's a way to offer convenience.
- Overriding is about a subclass redefining a method it inherits from its superclass with the same name and same parameters. It's a way to change behavior.
The following image provides a great visual summary of this difference.

Compile-Time vs. Runtime Polymorphism
This difference leads to a fundamental concept in OOP:
- Method Overloading is an example of compile-time polymorphism (or static binding). The compiler knows exactly which version of the
add()method to call based on the arguments you provide at the time you write the code. - Method Overriding enables runtime polymorphism (or dynamic binding). The decision of which method to execute (the superclass's or the subclass's) is made at runtime, based on the actual type of the object.
Let's watch a video that explains and demonstrates both concepts together.
Java Polymorphism Fully Explained In 7 Minutes
This video from "Coding with John" first explains runtime polymorphism through method overriding and then explicitly contrasts it with method overloading.
Please watch the following segments: Polymorphism and Overriding: This section introduces overriding using an Animal/Dog example, showing how the Dog class provides its own eat method. Method Overloading: Here, the video clearly defines method overloading as having the same method name but different parameters within the same class, providing a clear example.
To solidify your understanding, let's turn back to the index.dev article and study its detailed comparison table. This table is a perfect summary for study and interview preparation.
Overloading vs Overriding in Java: Differences, Examples ...
This resource neatly summarizes all the key differences in a structured format.
Focus on the table under the heading "Method Overloading vs Method Overriding: Key Differences". Read through each row to see how the two concepts differ in terms of their definition, polymorphism type, binding, parameters, and more.
4. Answering a Classic Interview Question
As a developer aiming for job-readiness, it's vital to be able to articulate this difference clearly. The question "What is the difference between method overloading and method overriding?" is a staple of Java interviews.
The article we've been using has a section dedicated to common interview questions on this topic. Reviewing it will help you prepare concise and accurate answers.
Overloading vs Overriding in Java: Differences, Examples ...
This section directly prepares you for job interviews by covering the most common questions related to this topic.
Read the section titled "Method Overloading and Method Overriding: Interview Questions". Pay special attention to the answers for overriding static or private methods and whether constructors can be overridden.
Based on what you've learned, here’s how you could structure your answer:
- Start with a definition: "Method overloading is when a class has multiple methods with the same name but different parameters. Method overriding is when a subclass redefines a method inherited from its parent class."
- Explain the purpose: "The purpose of overloading is to provide different ways to execute an operation, for convenience. The purpose of overriding is to provide a specific implementation of a method that is already provided by its superclass."
- Mention the type of polymorphism: "Overloading is resolved at compile-time (static polymorphism), while overriding is resolved at runtime (dynamic polymorphism)."
Conclusion
In this lesson, we've untangled two fundamental but often confused concepts in Java. You now have a clear framework for understanding, implementing, and explaining the difference between them.
Here are the key takeaways:
- Method Overriding: A subclass provides a new implementation for an inherited method. It requires the same method signature and an inheritance relationship. It enables runtime polymorphism.
- Method Overloading: A class contains multiple methods with the same name but different parameters. It is a form of compile-time polymorphism.
- The
@Overrideannotation is a crucial tool for ensuring your overrides are correct and your code is readable.
Method overriding is the mechanism that makes runtime polymorphism possible. In our next lesson, we will explore this powerful concept further, looking at upcasting and dynamic method dispatch to see how Java leverages overriding to write incredibly flexible and extensible programs.