Java Overload Method Example

This Java example overloads methods to improve program clarity and to optimize. A constructor is then overloaded.
Overloaded methods. In method overloading, two or more methods can share the same name. But they must have different arguments (based on types and count).
For program clarity, overloading has advantages. We can write smaller, more direct methods and then call them as needed. For performance, overloaded methods may also help.
First example. Here we use method overloading for the add() method. We can call add with two int arguments or two ints and a String.Strings

Tip: These add() methods are actually separate methods, with no shared code. They just share the method name.

Tip 2: With overloading, we can use simpler names (like "add") that are easier to remember, but that have separate implementations.

Java program that uses overloaded method public class Program { static int add(int value1, int value2) { // This method handles two int arguments. return value1 + value2; } static int add(int value1, int value2, String value3) { // This overload handles three arguments. return value1 + value2 + value3.length(); } public static void main(String[] args) { // Call overloaded add methods. int total = add(5, 5); total += add(10, 10, "carrot"); System.out.println(total); } } Output 36
Method, no overloads. This program does the same thing as the previous two add() methods. But it uses no overloading. Instead, it uses an if-statement and branches on the String argument.

Branch: The branch (from the if-statement) must be evaluated at runtime. This imposes a performance penalty.

If

Thus: This program has fewer methods than the previous one, but will likely execute slower.

Java program that uses no overloaded methods public class Program { static int add(int value, int value2, String value3) { // This method handles a null String with a branch. // ... It could be overloaded. if (value3 == null) { return value + value2; } else { return value + value2 + value3.length(); } } public static void main(String[] args) { // Call add() method twice. int total = add(5, 5, null); total += add(10, 10, "carrot"); System.out.println(total); } } Output 36
Constructor. Let us overload a constructor. Here we can create an Element instance with two arguments (an int and a String). We can also just use one int or one String.Constructors

Tip: As with methods, these overloaded constructors are separate. But we could call into a unified "initialize" method.

Java program that uses overloaded constructor class Element { int size; String color; public Element(int size, String color) { this.size = size; this.color = color; } public Element(int size) { this.size = size; this.color = "blank"; } public Element(String color) { this.size = 0; this.color = color; } public String toString() { // This puts field values into a String. return "size = " + this.size + ", color = " + this.color; } } public class Program { public static void main(String[] args) { // Create with two arguments. Element e = new Element(10, "blue"); System.out.println(e); // Use just one argument (size or color). e = new Element(50); System.out.println(e); e = new Element("red"); System.out.println(e); } } Output size = 10, color = blue size = 50, color = blank size = 0, color = red
Benchmark, overloads. Here we see how overloading can improve performance. With the add() overloads, we know what arguments are being used—we can act directly on them.

However: With the addNoOverload method, we must use an if-statement branch to determine the proper return expression.

Result: Calling the overloaded add methods is faster than using addNoOverload. Fewer branches are executed.

Java program that benchmarks overloaded methods public class Program { static int add(int value1, int value2) { return value1 + value2; } static int add(int value1, int value2, String value3) { return value1 + value2 + value3.length(); } static int addNoOverload(int value, int value2, String value3) { if (value3 == null) { return value + value2; } else { return value + value2 + value3.length(); } } public static void main(String[] args) { long t1 = System.currentTimeMillis(); // Version 1: use overloaded methods. for (int i = 0; i < 1000000000; i++) { int result = add(10, i) + add(5, i, "carrot"); if (result == 0) { return; } } long t2 = System.currentTimeMillis(); // Version 2: use method with if-statement. for (int i = 0; i < 1000000000; i++) { int result = addNoOverload(10, i, null) + addNoOverload(5, i, "carrot"); if (result == 0) { return; } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output 289 ms, add 329 ms, addNoOverload
A design decision. Overloading is an important design decision to make in programs. Excessive overloads are confusing. But when used correctly, they make programs clearer and often faster.
Reduce branches. If overloads can be used to reduce branches (eliminate ifs), they can improve program speed. This is a big advantage. With them, function names are easier to remember.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls