JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Author: Denise Roßberg
Program: Method Overloading
Similar to constructor overloading (see comments).
Ways to overload a method:
* different numbers of parameters
* difference in data types of parameters
* change sequence of data type of parameters
How it works:
The compiler must sort out which method to call. It picks the correct method by matching the parameter types of the method with the types of the values used in the method call.
Why overloading:
Without overloading each method would have a different name. In my example I have six -> six different method names: calcSum1, calcSum2 or calcSumInteger, calcSumDouble...It gets confusing.
With overloading you can choose one name for all.
About method signature:
To describe a method you need to specify its name together with its parameter types. This is the signature. The returning type is not part of it.
Valid method overloading examples:
calcSum(int, int)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run