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

Runtime & versions

Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Java Language Specification

Specifications & further reading