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

Method 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.

Method overloading is the ability to write the same method multiple times with different parameters and return values.

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

java

Main

copy
12345678910
static int[] flipArray(int[] array) { // method for flipping int array int[] result = new int[array.length]; int index = 0; for (int i = array.length - 1; i > 0; i--) { result[index] = array[i]; index++; } return result; }
java

Main

copy
12345678910
static char[] flipArray(char[] array) { // method for flipping char array char[] result = new char[array.length]; int index = 0; for (int i = array.length - 1; i >= 0; i--) { result[index] = array[i]; index++; } 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
123456789101112131415
static void printArrayToTheConsole(int[] array) { // method for printing int array for (int element : array) { System.out.print(element + " "); } System.out.println(); } static void printArrayToTheConsole(char[] array) { // method for printing char array for (char element : array) { System.out.print(element + " "); } System.out.println(); }

Here we have overloaded the method printArrayToTheConsole to accept and return int and char values. Note that if our method accepts a char type, it should return a char type. If the method accepts an int type, it should return an int type. 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
package com.example; public class Main { public static void main(String[] args) { // do not change code from 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)); } // do not change the code above static int[] flipArray(int[] array) { // method for flipping int array int[] result = new int[array.length]; int index = 0; for (int i = array.length - 1; i > 0; i--) { result[index] = array[i]; index++; } return result; } static char[] flipArray(char[] array) { // method for flipping char array char[] result = new char[array.length]; int index = 0; for (int i = array.length - 1; i >= 0; i--) { result[index] = array[i]; index++; } return result; } static void printArrayToTheConsole(int[] array) { // method for printing int array for (int element : array) { System.out.print(element + " "); } System.out.println(); } static void printArrayToTheConsole(char[] array) { // method for printing 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?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 2. Capítulo 6
We're sorry to hear that something went wrong. What happened?
some-alt