Method Overloading in Java

Published: Last Updated on 1.9K views 4 minutes read
A+A-
Reset

If we define a method with same name as an existing method in an class, then this concept is Method overloading. Beginners may think this as tough concept, But i found this very simple. The method overloading is way to achieve polymorphism.

Need Of Method Overloading:

here I want to give a scenario in which we have a method inside our class. Let the signature of method is int sum(int x, int y);

In this case we can add two byte, short, char or int values by passing them in this method. But what if we need to add float values or we need to concatenate strings in some fashion. Then we have to create a function with different name like double  sumdouble(double c, double d);

or String sumString(String r, String s );

Here we are compelled to give different names which are not meaningful. This reduce the readability of the code.

Readability is the main advantage of Method Overloading.

Rules for Method Overriding:

Lets see some rules for Method Overriding.

  1. All overloaded methods must have same name.
  2. In Method Overloading the signature of methods must not exactly same.
  3.  Overloaded methods must have
    • Differnt number of parameters
    • or Different types of parameters
  4. We can not overload methods using different return type. Means return type is not significant in Method Overloading.

Example1:

Lets have a look at following example.

OverloadMain.java

package overloading.main;

public class OverloadingMain {
	static void sum(int x,double y){
		System.out.println("int--double - Sum is "+(x+y));
	}
	 static void sum(double x,double y){
		System.out.println("double--double - Sum is "+(x+y));
	}
	static void sum(double x,int y){
			System.out.println("double--int - Sum is "+(x+y));
		}
	static void sum(int x,int y){
		System.out.println("int--int - Sum is "+(x+y));
	}
	static void sum(int x, int y, int z){
		System.out.println("int--int--int -- "+(x+y+z));
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		sum(12.1,21.1);
		sum(12,22);
		sum(11.2,11);
		sum(13,22.2);
		sum(22,1,2);
	}
}

In above program compiler checks the type of arguments and numbers of argument then decide, which method to call.

Output:

Output of the above program is like this.

double–double ->Sum is 33.2
int–int ->Sum is 34
double–int ->Sum is 22.2
int–double ->Sum is 35.2
int–int–int –> 25

Output Method Overloading

Output Method Overloading

Effect of Automatic Type promotion in Method Overloading

Due to automatic type promotion Overloaded method may behave some what differently.

Figure below will illustrate the automatic type promotion of java primitive data types.

Automatic type promotion in java

Automatic type promotion in java

 

Have a look in the following example;

Example 2:

Demo1.java

package overloading.main;

public class Demo1 {
	
	void sum(char a, double b){
		System.out.println("Char--double -- "+(a+b));
	}
	
	void sum(int a,char b){
		System.out.println("int--char -- "+(a+b));
	}

}

Inside main method of overloading.main.OverloadMain.java

Demo1 d1=new Demo1();
d1.sum('a', 12);

Here the second argument will be promoted to double and first method will be called.

Output:

Char–double –> 109.0

Note: 

If you try this code you will get compile time error.

d1.sum('a', 'A');

Hope this article is helpful for you to understand Method Overloading in Java.

 

If you find any mistake and query about post please comment below.

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.