/ Backend Development

Java Overriding and Overloading - ChapGPT 4o

Creator Image
Bashar Alshaibani
16 May 2024 -
4 min Reading time

In Java, method overriding and method overloading are two fundamental concepts in object-oriented programming that allow developers to define methods with the same name but different behaviors. Here's a detailed look at the rules and characteristics of each:

Method Overriding

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The overriding method in the subclass must have the same name, return type, and parameter list as the method in the superclass.

Rules for Method Overriding:

  1. Same Name and Signature: The method in the subclass must have the same name, return type (or a subtype), and parameter list as the method in the superclass.
  2. Access Modifier: The access level of the overriding method cannot be more restrictive than that of the overridden method. For example, if the superclass method is protected, the overriding method cannot be private.
  3. Return Type: The return type must be the same or a subtype (covariant return type) of the return type of the overridden method.
  4. Exceptions: The overriding method cannot throw broader or new checked exceptions than those declared by the overridden method.
  5. Annotation: It is good practice to use the @Override annotation to indicate that a method is intended to override a method in the superclass. This helps to catch errors at compile-time.
  6. Instance Methods: Only instance methods can be overridden. Static methods cannot be overridden; they can only be hidden.

Example of Method Overriding:

class SuperClass {
    void display() {
        System.out.println("Display method in SuperClass");
    }
}

class SubClass extends SuperClass {
    @Override
    void display() {
        System.out.println("Display method in SubClass");
    }
}

public class Test {
    public static void main(String[] args) {
        SuperClass obj = new SubClass();
        obj.display();  // Calls the display method in SubClass
    }
}

Method Overloading

Method overloading occurs when multiple methods in the same class have the same name but different parameter lists. Overloaded methods can have different return types and access modifiers.

Rules for Method Overloading:

  1. Different Parameter List: Overloaded methods must have different parameter lists (different number or types of parameters).
  2. Return Type: Overloaded methods can have the same or different return types.
  3. Access Modifier: Overloaded methods can have different access modifiers.
  4. Exceptions: Overloaded methods can throw different exceptions.
  5. Static and Instance Methods: Both static and instance methods can be overloaded.

Example of Method Overloading:

class Example {
    void display() {
        System.out.println("Display method with no parameters");
    }

    void display(int a) {
        System.out.println("Display method with one integer parameter: " + a);
    }

    void display(String s) {
        System.out.println("Display method with one string parameter: " + s);
    }
}

public class Test {
    public static void main(String[] args) {
        Example obj = new Example();
        obj.display();          // Calls display method with no parameters
        obj.display(10);        // Calls display method with one integer parameter
        obj.display("Hello");   // Calls display method with one string parameter
    }
}

Key Differences:

  • Overriding:

    • Occurs between superclass and subclass.
    • Requires the same method signature.
    • Enables runtime polymorphism (dynamic method dispatch).
  • Overloading:

    • Occurs within the same class.
    • Requires different method signatures.
    • Does not involve polymorphism.

Understanding these concepts and their rules is crucial for writing effective and maintainable Java code, leveraging polymorphism, and ensuring clear method interfaces.