What Are The Rules For Overriding

Private method can be overridden by private, friendly, protected or public

methods. Friendly method can be overridden by friendly, protected or public methods.

Protected method can be overridden by protected or public methods. Public method

can be overridden by public method.

In Java, method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already provided by its superclass. When overriding a method, certain rules need to be followed to ensure correct behavior. Here are the key rules for method overriding in Java:

  1. Method Signature: The overriding method in the subclass must have the same method signature (name, return type, and parameter types) as the overridden method in the superclass.
    java
    // Superclass
    class Superclass {
    void myMethod(int num) {
    // method implementation
    }
    }
    // Subclass
    class Subclass extends Superclass {
    // Overriding method with the same signature
    void myMethod(int num) {
    // overridden method implementation
    }
    }

  2. Access Modifier: The access level of the overriding method in the subclass cannot be more restrictive than the overridden method in the superclass. However, it can be less restrictive. The access modifiers are public, protected, default (no modifier), and private.
    java
    // Superclass
    class Superclass {
    protected void myMethod() {
    // method implementation
    }
    }
    // Subclass
    class Subclass extends Superclass {
    // Valid: overriding method can be protected or public
    public void myMethod() {
    // overridden method implementation
    }
    }

  3. Return Type: The return type of the overriding method in the subclass must be the same as, or a subtype of, the return type of the overridden method in the superclass. In Java 5 and later versions, it’s possible to use covariant return types.
    java
    // Superclass
    class Superclass {
    Number myMethod() {
    // method implementation
    return 0;
    }
    }
    // Subclass
    class Subclass extends Superclass {
    // Valid: overriding method has a subtype return type
    Integer myMethod() {
    // overridden method implementation
    return 0;
    }
    }

  4. Exception Handling: If the overridden method in the superclass throws any checked exceptions, the overriding method in the subclass must not throw any new or broader checked exceptions. It can throw a narrower or the same exception type.
    java
    // Superclass
    class Superclass {
    void myMethod() throws IOException {
    // method implementation
    }
    }
    // Subclass
    class Subclass extends Superclass {
    // Valid: overriding method can throw a narrower or the same exception
    void myMethod() throws FileNotFoundException {
    // overridden method implementation
    }
    }

  5. Static and Final Methods: Static methods cannot be overridden, and final methods cannot be overridden or hidden by subclasses.

These rules ensure that the subclass maintains the same interface as the superclass, providing a consistent and predictable behavior when working with polymorphism.