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+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.
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.
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,
Integeris a valid return type because it is a subtype of the reference typeNumber.
- If the superclass method is
protected, the subclass method must beprotectedorpublic. - Attempting to assign a
privateor package-private (default) modifier in this scenario will result in a compilation error.
- 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.,
FileNotFoundExceptioninstead ofIOException). - It may declare fewer checked exceptions, or none at all.
- It may throw any unchecked exception (
RuntimeExceptionand its subclasses, orErrorand 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:finalmethods: Thefinalkeyword explicitly prevents a method from being overridden by any subclass.privatemethods: 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.staticmethods: 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 thesuper 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()).
Master Java with Deep Grasping Methodology!Learn More





