7.5. Method Overloading

Signature: Each method has a signature, which comprises the name of the method, and the types and order of the parameters in the formal parameter list.

			package javatutorial;

			/**
			 * Example class to identify the method signature in Java
			 * 
			 * @author f5java.com
			 */
			public class MethodSignature {
				// 1. Sample methods to identify method signature
				
				public void firstMethod (int age, float salary, String name){
					System.out.println("Inside first method");
				}
				
				public void secondMethod (String name, String address){
					System.out.println("Inside second method");
				}
				
				private boolean thirdMethod (boolean input, double value){
					System.out.println("Inside third method");
					return input;
				}
				
				public void fourthMethod (String name , Car car){
					System.out.println("Inside fourth method");
				}
				

				public static void main(String[] args) {
					// Lets identify and print the signature of each method
					System.out.println("Printing method signatures");
					System.out.println("Signature of firstMethod is : firstMethod (int, float, String)");
					System.out.println("Signature of secondMethod is : secondMethod (String, String)");
					System.out.println("Signature of thirdMethod is : thirdMethod (boolean, double)");
					System.out.println("Signature of fourthMethod is : fourthMethod (String, Car)");
					
				}
			}
		

Method Overloading: Several methods in a class can have same name, if they have different signatures. Since overloaded methods have the same name, their parameter lists must be different. Rather than inventing new method names, method overloading can be used when the same logical operation requires multiple implementations.

			package javatutorial;

			/**
			 * Example class to learn about Method Overloading in Java
			 * 
			 * @author f5java.com   
			 */
			public class MethodOverloading {
				// Below are overloaded methods

				/**
				 * This method adds two integers 
				 */
				public void add (int number1, int number2){
					System.out.println("Adding int types");
				}
				
				/**
				 * This method adds two integers 
				 */
				
				// Overloaded method : same method name, different signature
				public void add (double number1, double number2){
					System.out.println("Adding double types");
				}
				
				/**
				 * This method adds bonus amount to employee salary
				 */
				
				// Overloaded method : same method name, different signature
				public void add(double bonusAmt, Employee employee){
					System.out.println("Adding bonus to salary");
				}
				
				public static void main(String[] args) {
					// 1. Instantiate and call the overloaded methods
					MethodOverloading overloadingExample = new MethodOverloading();
					System.out.println("Calling overloaded methods");
					
					// Method to be invoked is decided by matching the 
					// signature of the method and type of parameter passed as arguments
					
					// 2. calling double addition method
					overloadingExample.add(100d, 100d);
					
					// 3. calling int addition method
					overloadingExample.add(50, 50);
					
					Employee Jerry = new Employee();
					// 4. calling bonus addition method
					overloadingExample.add(5000d, Jerry);

				}

			}