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

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

java

Main.java

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

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

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?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 6

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

java

Main.java

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

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

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?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 6
some-alt