Home
Java
Overload Method Example
This page was last reviewed on Dec 5, 2022.
Dot Net Perls
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.
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.
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); } }
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.
Detail 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.
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); } }
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.
Constructor
Tip As with methods, these overloaded constructors are separate. But we could call into a unified "initialize" method.
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); } }
size = 10, color = blue size = 50, color = blank size = 0, color = red
Benchmark, overloads. Here we see how overloading can improve performance. We implement a feature with 2 separate designs: overloaded methods, and a nested if-statement.
Version 1 With the add() overloads, we know what arguments are being used—we can act directly on them.
Version 2 We call the addNoOverload method, which uses 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.
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); } }
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.