The Concept of Method Overloading in Java


Method overloading allows a class to have more than one method with the same name, as long as their parameter lists are different.

Source Code

public class Main {
    static int add(int a, int b) {
        return a + b;
    }
    static double add(double a, double b) {
        return a + b;
    }
    public static void main(String[] args) {
        System.out.println(add(3, 4));         // Outputs 7
        System.out.println(add(3.5, 4.5));     // Outputs 8.0
    }
}

Overloading methods provide flexibility and readability, allowing different variations of a method to be called with different types or numbers of parameters.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments