Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Java, method overriding lets a subclass provide its own implementation of an inherited instance method. When that method is called through a superclass or interface reference, Java selects the implementation for the object’s runtime class. Use @Override so the compiler can confirm that you really are overriding a method.

How method overriding works

An override requires an inherited instance method and a compatible instance method declaration in a subclass or implementing class. The reference type determines which methods are available at compile time; for an overridable instance-method call, the runtime object determines which implementation runs.

class Animal {
    String sound() { return "?"; }
}

class Dog extends Animal {
    @Override
    String sound() { return "woof"; }
}

Animal pet = new Dog();
System.out.println(pet.sound()); // woof

Although pet is declared as Animal, it refers to a Dog object, so the call runs Dog.sound(). This runtime selection is polymorphism. Oracle’s polymorphism tutorial demonstrates the same principle; Oracle notes that its tutorials were written for JDK 8, so use the Java SE 25 Language Specification for current language rules.

Why use @Override?

The annotation tells readers that the declaration is intended to override an inherited method, and asks the compiler to verify that relationship. If a name or parameter list is mistyped, compilation fails rather than quietly creating an unrelated overload. Consistently annotate overrides; see Oracle’s overriding and hiding tutorial.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Overriding, overloading, and static hiding

Concept What changes How Java selects the method
Overriding A subclass provides a compatible implementation of an inherited instance method. For an instance call, the runtime object’s class determines the implementation.
Overloading A method has the same name but a different parameter list. A subclass may overload an inherited method. The compiler resolves which overload applies from the call’s available types and arguments.
Static hiding A subclass declares a same-signature static method. The qualifying type at compile time determines the method; this is not runtime polymorphism.

A static method does not override an instance method, and an instance method does not override a static method. For static methods, use the class name to make the qualifying type explicit rather than calling the method through an object reference.

Rules an overriding method must satisfy

Java checks more than whether the method names match. The overriding declaration must have a subsignature compatible with the inherited method, and it must satisfy the inherited method’s contract.

  • Return type: A reference return type may be covariant: an override can return a subtype, such as Dog when the inherited method returns Animal. Primitive return types must match.
  • Access: An override cannot make the method less accessible. A public method must remain public; a protected method may remain protected or become public; package access cannot become private.
  • Checked exceptions: An override cannot add checked exceptions beyond what the inherited declaration allows.
  • Final methods: A method declared final cannot be overridden.
  • Inheritance and visibility: Confirm that the superclass declaration is inherited and visible to the subclass; private methods are not inherited as overridable methods.

These constraints are specified in Chapter 8 of the Java SE 25 Language Specification.

Methods that cannot be overridden

Private methods

A private superclass method is not inherited as an overridable method. A subclass may declare a method with the same name and parameters, but that declaration is separate; it does not replace the private method.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Final methods and classes

A final method cannot be overridden. A final class cannot be subclassed, so there can be no subclass override of its methods.

Constructors

Constructors are not inherited instance methods, so they cannot be overridden. A class may define multiple constructors with different parameter lists; that is overloading.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Overriding interface methods and resolving defaults

A class instance method can implement a compatible abstract instance method declared by an interface. Interface default methods are inherited subject to Java’s inheritance rules, and an appropriate inherited class instance method takes precedence over an interface default.

If unrelated interfaces provide conflicting default implementations with the same signature, the implementing type must resolve the conflict by overriding the method. The class can call a particular interface default implementation explicitly with InterfaceName.super.method(). Static interface methods are not inherited.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A quick check when an override fails to compile

  1. Confirm the parent declaration is an instance method that the subclass inherits and can access; private methods are not overridden.
  2. Compare the method name and parameter types. A changed parameter list usually means you have created an overload, not an override.
  3. Check return-type compatibility, access level, and checked exceptions against the inherited declaration.
  4. Check whether the parent method is final, or whether one declaration is static and the other is an instance method.
  5. For interface methods, identify whether the candidate is abstract, default, or static, and check whether another inherited method or conflicting default affects the result.
  6. Add @Override and compile again. The compiler will report when the declaration does not actually override a method.

Java overriding at a glance

  • Overriding applies to inherited instance methods; calls dispatch according to the runtime object.
  • A different parameter list creates an overload, not an override.
  • Same-signature static methods hide rather than override one another.
  • Use @Override to make intent clear and catch signature mistakes.
  • Return types, access, checked exceptions, inheritance, and final declarations constrain whether an override is valid.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.