Method Overloading in Java

Method Overloading in Java

Method overloading in Java is a very popular feature whereby you can declare the same name for two or more methods but the methods must have a varying parameter list. So the methods either need to have different number of parameters or different type of parameters, or both. Overloading is related to compile-time (static) polymorphism.

Sample Code

public class Sum
{
public int sum (int x, int y)
{
return (x + y);
}
public int sum (int x, int y, int z)
{
return (x + y + z);
}
public double sum (double x, double y)
{
return (x + y);
}
public static void main (String args [])
{
Sum s = new Sum ();
System.out.println (s.sum (10, 20));
System.out.println (s.sum (10, 20, 30));
System.out.println (s.sum (10.5, 20.5))
}
}

 

OUTPUT

30
60
31.0

//In the code above, you must have noticed three methods with the same name ‘sum’ has been used. However, the parameter list is not the same. When methods of these types are called, the parameter list is used to distinguish one method from the other.

Analysis

Some of the questions that may arise pertinent to this feature are what if the exact prototype does not match with the argument.

The compiler undergoes the following steps one after the other:

  1. Type conversion is carried out only to a higher type within the same family.
  2. Type conversion to the immediate higher family. For example, for an int data type, if there is no long type available, it will start looking for the next higher data type which is float.

Code:

class Demo
{
public void show (int x)
{
System.out.println (“Int int” + x);
}
public void show (String s)
{
System.out.println (“In String” + s);
}
pubic void show (byte b)
{
System.out.println (“In byte” + b);
}
}
class Demo1
{
public static void main (String args [])
{
byte a = 25;
Demo obj = new Demo ();
obj.show (a); //This will produce a byte argument
obj.show (“hello”); //String
obj.show (250); //The next higher datatype is int since char is not availabel
obj.show (“A”); //String
obj.show (7.5); //Since float is not available here, this will lead to a compilation error
}
}

 

Advantage of Method Overloading

A major advantage of this feature is that the same name can be used for various methods. Especially when a series of methods essentially perform the same function taking into consideration a few variations, it does not make sense to come up with different names to define the methods. If Java did not have overloading, the three sum methods in the first sample code of this article would have to be named sum1, sum2, and sum3.

Overloading of methods on return type

Methods cannot be overloaded on return type just like C++.

public class Main
{
pubic int foo ()
{
return 10;
}
public char foo()
{
return ‘a’;
}
public static void main (String args [])
{
}
}

However, when the particular data type is explicitly called, overloading based on return type can be carried out like in the example below:

public class Main
{
public static int foo (int a)
{
return 10;
}
public static char foo (int a, int b)
{
return ‘a’;
}
public static void main (String args [])
{
System.out.println (foo (1));
System.out.println (foo (1, 2));
}
}

 

OUTPUT
10
a

 

class A
{
public int foo (int a)
{
return 10;
}
public char foo (int a, int b)
{
return ‘a’;
}
}
public class Main
{
public static void main (String args [])
{
A a = new A ();
System.out.println (a.foo (1));
System.out.println (a.foo (1, 2));
}
}

 

OUTPUT
10
a

Overloading Static Methods

Static methods, like normal methods, can be overloaded in a similar manner in which the methods can have the same name but different parameter lists. However, two methods that only have the static keyword to differ from each other cannot be overloaded. The main () function of Java can also be overloaded. Follow the code below to understand the nuances of method overloading better.

import java.io.*;
public class Test
{
public static void main (String args [])
{
System.out.println (“Hello World”);
Test.main (“World”);
}
public static void main (String args [])
{
System.out.println (“Hello” + arg1);
Test.main (“World. It is a beautiful day”);
}
public static void main (String arg1, String arg2)
{
System.out.println (“Hello” + arg1 + “,” + arg2);
}
}

OUTPUT
Hello World
Hello World. It is a beautiful day
Hello World

Does Java support operator overloading?

Operator overloading is supported in C++. However, this feature is not enabled in Java. Therefore, an operator can only be used for the purpose it has been designated in the first place. However, this does not mean some operator overloading does not take place. Internally, Java uses the ‘+’ operator both for arithmetic operations as well as concatenation.

Difference between Overloading and Overriding?

  • As mentioned throughout the article, overloading is the process of having methods with the same name but different parameter lists.
  • Overriding is about the same function or method which will have the same signature but will be connected to different classes via inheritance.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.