Core Java - SPLessons

Overloading in Java

Chapter 17

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Overloading in Java

Overloading in Java

shape Description

Overloading in Java, If one class has multiple methods by similar name but, various parameters then it is called as method overloading, the advantage of Overloading in Java consists of the concept about readability of a program but, changing the return type is not possible in method Overloading in Java. Overloading in Java consists of 2 types, such as

shape Conceptual
Figure

Following are the types of method overloading.

shape Example

Method Overloading by changing number of parameters.

Overloading in Java, here developer is changing parameters of the program as below.
add.java

package corejava;

public class add {
	void sum(int a,int b)
{System.out.println(a+b);
}  
	  
void sum(int a,int b,int c)
{System.out.println(a+b+c);}  
	  
	  
public static void main(String args[])
{  
	  add obj=new add();  
	  obj.sum(100,100,100);  
	  obj.sum(200,200);  
	  
	  }  
}

Where developer used two methods with the same name that is void sum() and given different parameters such as int a, int b, int c.

void sum(int a,int b){System.out.println(a+b);}  
	  void sum(int a,int b,int c){System.out.println(a+b+c);}  

Where created one object called obj, by using this object method has been called.

obj.sum(100,100,100);  
	  obj.sum(200,200);  

When compile the code output will be as follows.

300
400

Method Overloading by changing the data type.

Overloading in Java, As explained in the above program where change the data type as double and float then observe the result.

package corejava;

public class add {
	
void sum(int a,int b)
{
System.out.println(a+b);
}  
	  
void sum(double a,float b,int c){
System.out.println(a+b+c);
}  
public static void main(String args[])
{  
	  add obj=new add();  
	  obj.sum(100,100,100);  
	  obj.sum(200,200);  
	  
	  }  
}

Where the developer has changed data type from integer to double and float.

void sum(int a,int b)
{
System.out.println(a+b);
}  
void sum(double a,float b,int c){
System.out.println(a+b+c);
}  

When compile the code following is the result will be generated.

300.0
400

Is method overloading possible by changing the return type.

No, Overloading in Java concept if any one removes the return type, then compile error will become as follows.

package corejava;

public class add {
	int sum(int a,int b){
		System.out.println(a+b);
		}  
	  double sum(double a,float b,int c){
		  System.out.println(a+b+c);
		  }  
	  
	  public static void main(String args[]){  
	  add obj=new add();  
	  obj.sum(100,100,100);  
	  obj.sum(200,200);  
	  
	  }  
}

This code can not be compiled why because it gives compile time error, there is no return type with a name void.

int sum(int a,int b)
double sum(double a,float b,int c)

Overloading in Java, main() overloading is possible here as follows.

public class overload {
	
	public static void main(int splesson){
		
		System.out.println(splesson);
	}
public static void main(String...args){
	System.out.println("Hello SPLessons");
	main(78);
}
}

In Java one can main method as follows also.

public static void main(String...args)

Where main() has been invoked, when compile the code output will be as follows.

Hello SPLessons
78

Overloading vs Overriding in Java


Summary

shape Key Points

  • Overloading in Java is nothing but having same method with various parameters.
  • Overloading is not possible once any one change the return type of the method.