Lesson 2 of 518 minModule progress 0%

Module 6: Inheritance and Polymorphism

Method Overriding and `@Override`

Change inherited behavior in a subclass while keeping the same method contract as the parent class.

Author

Java Learner Editorial Team

Reviewer

Technical review by Java Learner

Last reviewed

2026-04-16

Java version

Java 25 LTS

How this lesson was prepared: AI-assisted draft, edited by hand, and checked against current Java 25 documentation and runnable examples.

Learning goals

  • Override a parent method correctly
  • Use `@Override` to catch mistakes
  • Understand the difference between replacing and reusing behavior

Method overriding lets the child class provide its own version of a parent method: Same method name, same parameters, new behavior.

@Override is a safety check: It tells the compiler that you really intend to override a parent method.

This is how child classes become specific: A generic Animal may make a sound, but each concrete animal makes a different sound.

Good beginner habit: Always use @Override when you mean to override.

Runnable examples

A child class replaces the parent behavior

class Animal {
    void makeSound() {
        System.out.println("Some animal sound");
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Woof");
    }
}

Expected output

Calling `makeSound()` on a Dog uses the Dog version.

Common mistakes

Changing the parameters and thinking the method was overridden

That creates a new overload, not an override. Keep the same parameter list.

Mini exercise

Override a `start()` method in a `Car` class so it prints a more specific message than the parent `Vehicle` class.

Summary

  • Overriding replaces inherited behavior in a child class.
  • `@Override` helps the compiler catch mistakes.
  • The method signature must stay the same.

Next step

Now combine inheritance and overriding to see polymorphism in action.

Sources used

Advertisement

Lesson check

Why should you use `@Override`?

Next lesson →