Java Program to find Arithmetic Sum using Method Overloading

Write a Java program to find arithmetic sum using method overloading concept. In this example, we created four methods of the same name with different arguments to perform arithmetic addition or sum. Every time we call the add function, Javac will call the appropriate method based on the arguments. 

package NumPrograms;

public class SumMethodOverloading {
	
	int add(int a, int b)
	{
		int sum = a + b;
		return sum;
	}
	
	int add(int a, int b, int c)
	{
		return a + b + c;
	}
	
	int add(int a, int b, int c, int d)
	{
		return a + b + c + d;
	}
	
	int add(int a, int b, int c, int d, int e)
	{
		return a + b + c + d + e;
	}
	
	public static void main(String[] args) {
		
		SumMethodOverloading obj = new SumMethodOverloading();
		
		int sum = obj.add(10, 20);
		
		System.out.println("The Sum of Two Numbers   = " +  sum);
		
		System.out.println("The Sum of Three Numbers = " + obj.add(10, 20, 40));
		
		System.out.println("The Sum of Four Numbers  = " + obj.add(15, 25, 49, 66));
		
		System.out.println("The Sum of Five Numbers  = " + obj.add(11, 19, 16, 72, 66));

	}

}
Java Program to find Arithmetic Sum using Method Overloading

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.