Codectionary / Developer documentation / Java

Method Overriding & @Override

Method overriding lets a subclass provide its own specific implementation of a method already defined in its parent class, replacing the inherited behavior. The overriding method must have the same name, return type (or a covariant subtype), and parameter list as the parent's version. The @Override annotation isn't strictly required, but it tells the compiler to verify you're actually overriding a real parent method, catching typos at compile time instead of silently creating an unrelated new method.

Syntax

@Override\nreturnType methodName(parameters) {\n  // new implementation\n}

Examples

Basic Method Overriding

A subclass replacing a parent's method with its own version.

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

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Meow!");
    }
}

Animal animal = new Cat();
animal.makeSound();  // Meow! - the overridden version runs

Why @Override Matters

@Override catches a common mistake: a typo that accidentally creates a new method instead of overriding the intended one.

class Shape {
    public double calculateArea() {
        return 0;
    }
}

class Circle extends Shape {
    private double radius = 5;

    @Override
    public double calculateArea() {  // correctly overrides
        return Math.PI * radius * radius;
    }

    // @Override
    // public double calculatearea() {  // typo! lowercase 'a'
    //     return Math.PI * radius * radius;
    // }
    // Without @Override, this typo would silently compile as a brand new,
    // unrelated method instead of overriding calculateArea().
}

Calling the Parent Version with super

A subclass can extend, rather than fully replace, the parent's behavior by calling super.method().

class Employee {
    public void work() {
        System.out.println("Doing general work tasks");
    }
}

class Manager extends Employee {
    @Override
    public void work() {
        super.work();  // still do the general work first
        System.out.println("Also managing the team");
    }
}

Manager m = new Manager();
m.work();
// Doing general work tasks
// Also managing the team

Best practices

  • Always add @Override when overriding a method - it lets the compiler catch signature mistakes that would otherwise fail silently
  • Keep the overriding method's behavior consistent with what the parent class's documentation promises (the Liskov Substitution Principle)
  • Use super.methodName() inside an override when you want to extend the parent behavior rather than fully replace it
  • Remember private, static, and final methods cannot be overridden - only inherited public/protected instance methods can be

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Classes
Classes in Java are blueprints for creating objects, defining their properties (fields) and behaviors (methods). Java is a strictly object-oriented language where everything is encapsulated within classes. A class serves as a template that specifies what data an object will store and what operations it can perform.
Interfaces
An interface defines a contract of methods that implementing classes must provide, without specifying how those methods work internally. Unlike a class, an interface cannot be instantiated directly - it only declares what a class can do, not how. A class uses the 'implements' keyword to fulfill an interface's contract, and a single class can implement multiple interfaces, which is Java's way of achieving a form of multiple inheritance.
Abstract Classes
An abstract class is a class that cannot be instantiated directly and may contain both fully implemented methods and abstract methods (declared without a body, which subclasses must implement). Abstract classes sit between interfaces and regular classes: like an interface, they define a contract; like a regular class, they can hold state (fields) and provide shared, already-implemented behavior.
Constructors
A constructor is a special method that runs automatically when an object is created with 'new', typically used to initialize the object's fields. A constructor shares its name with the class and has no return type, not even void. Java supports constructor overloading, letting a class offer multiple ways to construct an object, and constructor chaining with this(...), where one constructor calls another in the same class.