01 / 21

Method
Overriding in Java

Learn how subclasses redefine inherited methods to provide specialized behavior, and master the @Override annotation for safer code.

Java OOP

What is Method Overriding?

When a subclass provides its own implementation of a method that is already defined in its parent class, we call that method overriding. The subclass method replaces the inherited version at runtime.

Why Override Methods?

Specialization
A Dog barks, a Cat meows: both override speak() from Animal.
Polymorphism
Call the same method on different objects and get different behavior at runtime.
Extensibility
Frameworks define base methods: you override them to plug in your logic.

Inheritance: The Foundation

Animal + speak(): String + move(): String extends Dog @Override + speak(): String

Rules of Overriding

1
Same method name as the parent class method.
2
Same parameter list: identical types, order, and count.
3
Same or covariant return type: the return type can be a subclass of the parent's return type.
4
Access modifier must be the same or less restrictive (e.g., protected to public).
5
Cannot override static, final, or private methods.

The @Override Annotation

@Override is an annotation you place above a method to tell the compiler: "I intend this method to override a parent method." If the signature doesn't match any parent method, the compiler throws an error instead of silently creating a new method.

@Override public String speak() { return "Woof!"; }

Overriding vs Overloading

Overriding Overloading
Classes involved Parent and child Same class (or child)
Method name Same Same
Parameters Must be identical Must differ
Return type Same or covariant Can differ
Binding Runtime (dynamic) Compile-time (static)
@Override ✓ Applies ✕ Does not apply

How to Override a Method

1
Identify the parent method you want to customize. Check its signature, return type, and access modifier.
2
In the subclass, write a method with the exact same signature and a compatible return type.
3
Add @Override above the method. If the compiler complains, fix the mismatch.
4
Implement the new body. Optionally call super.methodName() to reuse parent logic.
5
Test with polymorphism: declare a parent-type variable, assign a child object, and call the method.

Example 1: Basic Override

class Animal { public String speak() { return "..."; } } class Dog extends Animal { @Override public String speak() { return "Woof!"; } }

Example 2: Polymorphism in Action

Animal myPet = new Dog(); System.out.println(myPet.speak()); // Output: Woof!
Variable type Animal refers to Actual object Dog JVM calls Dog's speak() at runtime

Example 3: Calling super

class Shape { public String describe() { return "I am a shape"; } } class Circle extends Shape { @Override public String describe() { return super.describe() + " called Circle"; } }

Covariant Return Types

Since Java 5, an overriding method can return a subtype of the parent method's return type. This is called a covariant return type.

class Factory { public Animal create() { return new Animal(); } } class DogFactory extends Factory { @Override public Dog create() { // Dog is subtype of Animal return new Dog(); } }

Access Modifier Rules

Less restrictive ←────→ More restrictive public protected default private Override can go this direction (same or wider)

What You Cannot Override

final methods
Declared final to prevent any subclass from changing the behavior.
static methods
Static methods belong to the class, not objects. Redefining one is method hiding, not overriding.
private methods
Not visible to subclasses, so they cannot be overridden. A same-name method in the child is a brand-new method.

Common Mistakes

✕ Changing Parameters
Adding or changing a parameter creates an overloaded method, not an overridden one. The parent method still runs.
✕ Restricting Access
Making a public parent method protected in the child causes a compile error. Access can only widen.
✕ Forgetting @Override
Without @Override, a typo in the method name silently creates a new method. Bugs go undetected until runtime.

Summary Checklist

Requirement Valid Override Invalid Override
Method name Identical Different name
Parameters Identical Changed types/count
Return type Same or covariant Unrelated type
Access Same or wider More restrictive
Parent method Non-static, non-final, non-private static / final / private
@Override Recommended always Omitted (risky)

Question 1 of 5

Which of the following is required for a valid method override in Java?

Question 2 of 5

What happens if you add @Override to a method that does not actually override a parent method?

Question 3 of 5

A parent class has: protected void draw(). Which of these is a valid override in the child class?

Question 4 of 5

Given:

Animal a = new Cat(); a.speak();

If Cat overrides speak(), which version of speak() is called?

Question 5 of 5

What does super.methodName() do inside an overriding method?