Methods are used in Java to describe the behavior of an object. Methods are a collection of statements that are group together to operate. In Java, it is possible to create methods that have the same name, but different argument lists in various definitions, i.e., method overloading is possible in Java, which is one of the unique features of Object Oriented Programming (OOP). In this chapter, we will learn about how method overloading is written and how it helps us within a Java program.
What is Method Overloading in Java?
If a class of a Java program has a plural number of methods, and all of them have the same name but different parameters (with a change in type or number of arguments), and programmers can use them to perform a similar form of functions, then it is known as method overloading. If programmers what to perform one operation, having the same name, the method increases the readability if the program.
Let us take an example where you can perform multiplication of given numbers, but there can be any number of arguments a user can put to multiply, i.e., from the user point of view the user can multiply two numbers or three numbers or four numbers or so on. If you write the method such as multi(int, int) with two parameters for multiplying two values, multi(int, int, int), with three parameters for multiplying three values and so on. A programmer does method overloading to figure out the program quickly and efficiently. Method Overloading is applied in a program when objects are required to perform similar tasks but different input parameters. Every time an object calls a method, Java matches up to the method name first and then the number and type of parameters to decide what definitions to execute. This process of assigning multiple tasks to the same method is known as Polymorphism. There are different ways to overload a method in Java. They are:
- Based on the number of parameters: In this case, the entire overloading concept depends on the number of parameters that are placed within the parenthesis of the method.
- Based on the data type of the parameter: With the arrangement of data type, within the parameter, overloading of the method can take place.
- Based on the sequence of data types in parameters: The method overloading also depends on the ordering of data types of parameters within the method.
To create overloaded methods, programmers need to develop several different method definitions in the class, all have the same name, but the parameters list is different. Here is a sample code snippet:
Syntax:
public class Student {
public void add(int i, int j) {
....
}
public void add(int i) {
....
}
}
The static polymorphism is also called compile-time binding or early binding. Static binding happens at compile time, and Method overloading is an example of static binding where binding of the method call to its definition occurs at Compile time.
Program for Method Overloading in Java
Example:
class Multiply {
void mul(int a, int b) {
System.out.println("Sum of two=" + (a * b));
}
void mul(int a, int b, int c) {
System.out.println("Sum of three=" + (a * b * c));
}
}
public class Polymorphism {
public static void main(String args[]) {
Multiply m = new Multiply();
m.mul(6, 10);
m.mul(10, 6, 5);
}
}
Output:
Sum of two=60 Sum of three=300
Why Method Overloading is not possible if the return type of method is changed?
In Java, method overloading is not possible by changing the return type of the method because there may arise some ambiguity. Let's see how ambiguity may occur:
Example:
class overloadRetType {
int sum(int g, int h) {
System.out.println(g + h);
}
double sum(int g, int h) {
System.out.println(g + h);
}
public static void main(String args[]) {
overloadRetType ob = new overloadRetType();
int result = ob.sum(20, 20);
//The above line will produce a compile Time Error ....
}
}
The above program will generate a compile-time error. Here, Java cannot determine which sum() method to call and hence creates an error if you want to overload like this by using the return type. Overloading methods offer no specific benefit to the JVM, but it is useful to the program to have several ways do the same things but with different parameters.
Here are some other examples to show Method Overloading:
Program to Demonstrate Method Overloading Based on the Number of Parameters
Example:
class DispOvrload
{
public void show(char ch)
{
System.out.println ("You have typed the letter: "+ch);
}
public void show(char ch, char ch1)
{
System.out.println("You have typed the letter: "+ch+", and " + ch1);
}
}
class Main
{
public static void main (String args[] )
{
DispOvrload o1 = new DispOvrload();
o1.show('G');
o1.show( 'S', 'J' );
}
}
Output:
You have typed the letter: G You have typed the letter: S, and J
Program to Demonstrate Method Overloading Based on the Sequence of Data Type in the Parameters
Example:
class DispOvrload
{
public void show(char ch, int numb)
{
System.out.println ("The 'show method' is defined for the first time.");
}
public void show(int numb, char ch)
{
System.out.println ("The 'show method' is defined for the second time." );
}
}
class Main
{
public static void main (String args[] )
{
DispOvrload o1 = new DispOvrload();
o1.show('G', 62);
o1.show(46, 'S');
}
}
Output:
The 'show method' is defined for the first time. The 'show method' is defined for the second time.
Advantages of Method Overloading
- It is used to perform a task efficiently with smartness in programming.
- It increases the readability of the program.
- The Method overloading allows methods that perform proximately related functions to be accessed using a common name with slight variation in argument number or types.
- They can also be implemented on constructors allowing different ways to initialize objects of a class.
Disadvantages of Method Overloading
- It's esoteric. Not very easy for the beginner to opt this programming technique and go with it.
- It requires more significant effort spent on designing the architecture (i.e., the arguments' type and number) to up front, at least if programmers' want to avoid massive code from rewriting.
Method Overloading in Java - Video Tutorial
To understand "Java Method Overloading" in more depth, please watch this video tutorial.