> ## 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.

# Java Overriding Method

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.

```java theme={"dark"}
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()`).

```java theme={"dark"}
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;
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
