ADVERTISEMENT
ADVERTISEMENT

Home » Java programs

Java program to demonstrate example of method overloading

In this java program, we are going to learn method overloading? How to implement method overloading using java program?
Submitted by Preeti Jain, on March 17, 2018

Write a program to demonstrate the behavior of method overloading in java?

In method overloading, there are three ways to identify,

  1. Same method with different argument
  2. Number of argument and type of argument
  3. If number of argument and type of argument are same then we will check the order of type

Java program for method overloading

/* Demonstrate the behavior of library function */

class DemonstrateOverloading{
	public static  void main(String[] args){
		/* Display and call sum method with two argument */	
		System.out.println("Sum of two number is :" +
			DemonsOverloading.sum(2,3));

		/* Display and call sum method with three argument */	
		System.out.println("Sum of three number is :" +
			DemonsOverloading.sum(2,3,4));

		/* Display and call sum method with three argument */	
		System.out.println("Sum of four number is :" +
			DemonsOverloading.sum(2,3,4,5)); 

		/* Display and call sum method with four argument */	
		System.out.println("Sum of five number is :" +
			DemonsOverloading.sum(2,3,4,5,6));  
	}
}


class DemonsOverloading{
	/* Definition of sum() method with two argument */
	static int sum(int a, int b){
		return(a+b);    
	}
	
	/* Definition of sum() method with three argument */
	static int sum(int a, int b,int c){
		return(a+b+c);    
	}
	
	/* Definition of sum() method with four argument */
	static int sum(int a, int b,int c,int d){
		return(a+b+c+d);    
	}
	
	/* Definition of sum() method with five argument */
	static int sum(int a, int b,int c,int d,int e){
		return(a+b+c+d+e);    
	}
}

Output

D:\Java Articles>java DemonstrateOverloading
Sum of two number is :5
Sum of three number is :9
Sum of four number is :14
Sum of five number is :20
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.