Conteúdo do Curso
Java Extended
Java Extended
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:
Main
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; }
Main
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:
Main
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:
Main
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.
Obrigado pelo seu feedback!