Method Overloading in Java

Published by user on

Method Overloading in Java is a concept that allows a class to have more than one method with the same name but different signatures.

When we say signature, it includes method name and parameters.

Overloading is an example of compile-time or static polymorphism.

How to achieve Method Overloading:

In this section, we will go through the different ways to do method overloading. So at the end of this section, you have a good idea about the topic.

Changing the number of parameters

As we see in the below example, the print() accept the different numbers of parameters.  As a result, it gets overloaded.

 
	// prints only name
	public void print(String name) {
		System.out.println("Name is : " + name);
	}

	// prints name and gender
	public void print(String name, String gender) {
		System.out.println("Name is : " + name + " and Gender is : " + gender);
	}

 

Method Overloading in Java

Changing data type of parameters

In the below example, both the add() take different data types.

  private float add(float a, float b) {
    return a + b;
  }

  private double add(double x, double y) {
    return x + y;
  }

Changing the order of data type of parameters

We can also do overloading by swapping the data types of parameters.

  public double sum(float x, double y) {
    return x + y;
  }

  public double sum(double x, float y) {
    return x + y;
  }

Overloading Questions and Answers:

Can we overload the main() method in Java?

Yes. You can overload the main() in Java, but JVM will start execution from the main() that accepts String array.

Let’s have a look at the example.

public class OverloadingMain {

  // accepts char
  public static void main(char ch) {
    System.out.println("Called with char : " + ch);
  }

  // JVM starts execution from here
  public static void main(String[] args) {
    System.out.println("main with String[] args is called.");
  }
}

What if we pass a value to the method which is of different types?

There is a concept of type promotion, where a data type gets converted to a broader data type.

Let’s understand by example.

// Java program to show data type promotion
public class TypePromotion {

  public void display(int a) {
    System.out.println("display with int parameter called : " + a);
  }

  // it accepts double parameter
  public void display(double a) {
    System.out.println("display with double parameter called : " + a);
  }

  public static void main(String[] arguments) {
    TypePromotion typePromotion = new TypePromotion();
    byte a = 10;
    typePromotion.display(a);
    float f = 10.00f;
    typePromotion.display(f);
  }

}

Output:

display with int parameter called : 10
display with double parameter called : 10.0

How type-promotion works:

We call display() with byte argument. But TypePromotion class doesn’t have any method that accepts byte. So byte gets converted to the int, and hence display() method with int gets called.

The same logic applies to the float data type.

Can we overload a method in java based on the return type?

No. We cannot overload only based on the return types because the compiler gets confused about which method to call.

Let’s have a look at the example.

// Java program to show overloading 
// cannot happen only by changing return type 
public class OverloadByReturnType {

   public float multiply(float a, float b) {
   	return a * b;
   }

   public double multiply(float a, float b) {
 	return a * b;
   }
}

The above program raises compiler error.
The first multiply() returns a float, whereas the second returns double. So we cannot overload by changing the return type.

Summary:

In this article we saw.

  1. How the method overloading works and ways to do it is.
  2. Overloading makes the class readable and clean.
  3. We can overload the Java main() method, but execution always starts from the main(String[] args).
  4. We can also overload static methods.
  5. Finally, overloading is not possible just by changing the return type.

I hope you enjoyed the article. If you like it, please share.

For Further Reference:
https://en.wikipedia.org/wiki/Function_overloading

Categories: Java