Method Signature
- public class MethodOverLoading : This line defines a public class named MethodOverLoading.
- public static void main(String[] args) : This line defines the main method, which serves as the entry point of the program.
- addNumbers(3, 4);: This line calls the addNumbers method with two integer arguments: 3 and 4.
- addNumbers(2.5, 6);: This line calls the addNumbers method with a double argument:2.5 and an integer argument: 6
- addNumbers(7, 1.9);: This line calls the addNumbers method with an integer argument: 7 and a double argument: 1.9.
There are overloaded methods that match this combination, one with (int, int) and the other with (double, int) and another with ( int, double)
- public static void addNumbers(int a, int b) { … }: This method is an overloaded version of addNumbers that takes two integer parameters and calculates their sum.
- public static void addNumbers(int a, double b) { … }: This method is another overloaded version of addNumbers that takes an integer and a double parameter and calculates their sum.
- public static void addNumbers(double a, int b) { … }: This method is the third overloaded version of addNumbers that takes a double and an integer parameter and calculates their sum.
To summarize, the code demonstrates method overloading with three versions of the addNumbers method that deal with diverse parameter combinations.
Output:
Result of two num 7
Result of two num 8.5
Result of two num 8.9
It provides flexibility because you can manage various types of data or number of parameters with the same method name. It promotes code reuse, reducing code duplication and making it easier to maintain.
Visited 1 times, 1 visit(s) today