Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Method Overloading | Methods
Java Extended
course content

Contenido del Curso

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

bookMethod Overloading

Using a method with different parameters

Java allows us to write different methods with the same name. These methods can have different return types and/or parameter types. This is done so that we can use these methods with different types. Let's take the example of a method to print an array:

We can write this method to accept values of type int, but what if we need to print an array of type char? Do we have to write a method with a different name?

Java provides method overloading for this purpose.

Let's take a look at an example of printing data and reversing an array from the previous chapter:

java

Main

copy
123456789101112131415161718192021222324252627
// method to reverse an int array static int[] flipArray(int[] array) { // creating a new array to store the reversed elements int[] result = new int[array.length]; int index = 0; // iterating over the input array in reverse order for (int i = array.length - 1; i > 0; i--) { result[index] = array[i]; index++; } // returning the reversed int array return result; } // method to reverse a char array static char[] flipArray(char[] array) { // creating a new array to store the reversed elements char[] result = new char[array.length]; int index = 0; // iterating over the input array in reverse order for (int i = array.length - 1; i >= 0; i--) { result[index] = array[i]; index++; } // returning the reversed char array return result; }

We overloaded the method flipArray to accept and return different parameters: int and char. If we invoke this method, it can accept and return both int and char types.

Now let's overload the method to display an array on the screen. This method should also accept and return int and char types:

java

Main

copy
12345678910111213141516171819
// method to print an int array static void printArrayToTheConsole(int[] array) { // iterating through each element of the int array for (int element : array) { System.out.print(element + " "); } // printing a blank line after the array System.out.println(); } // method to print a char array static void printArrayToTheConsole(char[] array) { // iterating through each element of the char array for (char element : array) { System.out.print(element + " "); } // printing a blank line after the array System.out.println(); }

Here we have overloaded the method printArrayToTheConsole to accept and return int and char values.

Now let's combine the overloaded methods and use them in the main method for an array of int type and an array of char type:

java

Main

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
package com.example; public class Main { // main method to run the application public static void main(String[] args) { // do not modify the code below int[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; char[] charArray = {'s', 'b', 'c', 'd', 'e', 'f', 'g'}; printArrayToTheConsole(flipArray(intArray)); printArrayToTheConsole(flipArray(charArray)); } // method to reverse an int array static int[] flipArray(int[] array) { int[] result = new int[array.length]; int index = 0; // iterating over the array in reverse order for (int i = array.length - 1; i > 0; i--) { result[index] = array[i]; index++; } return result; } // method to reverse a char array static char[] flipArray(char[] array) { char[] result = new char[array.length]; int index = 0; // iterating over the array in reverse order for (int i = array.length - 1; i >= 0; i--) { result[index] = array[i]; index++; } return result; } // method to print an int array to the console static void printArrayToTheConsole(int[] array) { // printing each element of the int array for (int element : array) { System.out.print(element + " "); } System.out.println(); } // method to print a char array to the console static void printArrayToTheConsole(char[] array) { // printing each element of the char array for (char element : array) { System.out.print(element + " "); } System.out.println(); } }

We have overloaded two different methods by adding the ability to use these methods with arrays of type char. We have also slightly changed the names of these methods, as now we can overload them multiple times to accept and return values of different types.

To verify this, we created two different arrays, one of type int and the other of type char, and then passed these arrays to the overloaded methods, obtaining the correct results.

By the way, you have already encountered overloaded methods before. For example, the String method substring() is overloaded and can have either one parameter, int beginIndex, or two parameters, int beginIndex, int endIndex. Method overloading is very useful and is commonly used in practice.

How many times can we overload a method?

How many times can we overload a method?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6
some-alt