From the course: Oracle Java Certification: 3. Methods and Inheritance

Method overloading - Java Tutorial

From the course: Oracle Java Certification: 3. Methods and Inheritance

Start my 1-month free trial

Method overloading

- [Instructor] In Java, method names can be reused in the same class. It is called method overloading. Because of the common name, all overloaded methods must have different signatures for them to be distinguishable. This means they need to have different numbers of arguments, different argument types, arguments in different orders, or all three. When resolving overloaded method calls, Java always attempt to find the most specific match. It can perform, at most, one type conversion in order to find a match. Let's look at an example. This Java class defines two methods by overloading the print method name. The main method makes two method calls using the same method name. So the output from the program will depend on which method definition better matches each method call. Which method definition do you think will match the first method call at line three? The input parameter is an int. It doesn't match the input parameter of either method definition exactly. Recall that Java can perform one type conversion in order to find a match. In order to match this method call to the method definition at line seven, we must promote the int 42 to a long int and then box the long int into a long integer object. Whereas to match this method call to the second definition at line 11, it takes one conversion from an int to an integer object. Therefore, the second definition is a closer match. Now let's look at the second method call. The input parameter is a long int. Once it is converted to a long integer object, the method call can match either definition. After the type conversion, the method call matches the first definition at line seven exactly, but it only matches the second definition if we cast the long integer type to the big O object type. Therefore, the first definition will be used because it is a more specific match. Let's run this program to verify our expectations. As you can see, indeed the first call invokes the second definition and the second call uses the first definition. Another common use of method overloading is in constructor overloading. By definition, all constructors must have the same method name. Overloaded constructors are in fact independent methods and therefore they can call each other. Let's look at an example. In this class, we have a default constructor defined at line four and an overloaded constructor at line six. The default constructor seems to use the overloaded constructor to set a default message. There's a problem in this code. Now what could it be? It turns out that the default constructor definition won't compile because as a rule, if a constructor calls another constructor the call must appear as the first statement. This rule is violated in this example because the constructor call at line six is the second statement in the default constructor. This rule ensures that an object is properly constructed before anything can be done to it. If we run this program, we can see a clear message from the compiler about the problem.

Contents