Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Method overriding in Java occurs when a subclass or implementing class provides a specific implementation for a method that is already defined in its parent class or inherited from an interface (including abstract methods and Java 8+ default methods). It is the primary mechanism by which Java achieves runtime polymorphism (dynamic method dispatch), allowing the Java Virtual Machine (JVM) to resolve method calls based on the actual object instance at runtime, rather than the reference type declared in the code.
import java.io.IOException;
import java.io.FileNotFoundException;

class Superclass {
    public Number processData(String input) throws IOException {
        // Base implementation
        return 0;
    }
}

class Subclass extends Superclass {
    @Override
    public Integer processData(String input) throws FileNotFoundException {
        // Overridden implementation
        return 1;
    }
}

Technical Rules for Overriding

To successfully override a method, the subclass implementation must adhere to strict compiler rules regarding its signature, return type, access modifiers, and exception declarations. 1. Method Signature The method name and the parameter list (number, type, and order of parameters) must exactly match the method in the parent class or interface. If the parameter list differs, the method is being overloaded, not overridden. 2. Return Types
  • Primitive Types: The return type of the overriding method must be strictly identical to the return type declared in the overridden method.
  • Reference Types: Java supports covariant return types, meaning the return type can be identical to, or a subtype of, the return type declared in the parent method. In the syntax example above, Integer is a valid return type because it is a subtype of the reference type Number.
3. Access Modifiers The overriding method cannot restrict the access visibility of the overridden method. It can only maintain or widen the visibility.
  • If the superclass method is protected, the subclass method must be protected or public.
  • Attempting to assign a private or package-private (default) modifier in this scenario will result in a compilation error.
4. Exception Handling Restrictions apply specifically to checked exceptions:
  • The overriding method cannot throw new or broader checked exceptions than those declared by the overridden method.
  • It may throw narrower (subclass) checked exceptions (e.g., FileNotFoundException instead of IOException).
  • It may declare fewer checked exceptions, or none at all.
  • It may throw any unchecked exception (RuntimeException and its subclasses, or Error and its subclasses), regardless of the parent method’s declaration.

Non-Overridable Methods

Certain methods in Java cannot be overridden due to their binding or access level:
  • final methods: The final keyword explicitly prevents a method from being overridden by any subclass.
  • private methods: Because private methods are not inherited by subclasses, they cannot be overridden. If a subclass defines a method with the exact same signature as a private method in the superclass, it is treated as a completely new, independent method.
  • static methods: Static methods are resolved at compile-time (static binding) based on the reference class, not the object instance. If a subclass defines a static method with the same signature as a static method in the superclass, it results in method hiding, not method overriding.

The @Override Annotation

While not strictly required by the compiler, applying the @Override annotation to the subclass method is a standard technical practice. It instructs the compiler to verify that the annotated method successfully overrides a method in a superclass or implements a method from an interface. If the signature does not match a parent method, the compiler throws an error, preventing subtle bugs caused by accidental overloading.

Accessing the Overridden Method

A subclass can invoke the parent’s implementation of the overridden method using the super keyword. For overridden interface default methods, the syntax is InterfaceName.super.methodName(). This is strictly limited to the immediate parent class or interface; Java does not support chained super calls (e.g., super.super.method()).
import java.io.IOException;

class Subclass extends Superclass {
    @Override
    public Number processData(String input) throws IOException {
        // Invokes the implementation from Superclass
        Number baseResult = super.processData(input); 
        
        // Additional subclass logic
        return baseResult.intValue() + 1;
    }
}
Master Java with Deep Grasping Methodology!Learn More