Method Overloading in Java

Method Overloading in Java

 

Method Overloading in Java. In this Article i will explain what is an overloading in Java by stepping through the sample program below

In the sample program below, first  I will create a  class named “overloadMain ” .Then i will create 2 Method with same Method name , but different number of parameter.

There Are 3 Way s to do method Overloading in Java

  1. Numbers of Different parameter in the arguments
  2. Different of Data Type in Arguments
  3. Different sequence of data type of Arguments

Things to take note about Overloading in Java

  1. Method Overloading only perform inside a Class.
  2. Return Type can be the Same or different .
  3. Method Parameter should be different

 

Class overloadMain 

  1. First I create 2 Method with the same  Method Name “public static void worker”
  2. The Difference between the First Method and the second method are , different Parameter type.
  3. Then I called the Method at Main

 

package overloadNow;

public class overloadMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		worker("Marie");
		worker("John",15);

	}
	
	
	
	public static void worker(String nameWorker ){
		
		String workerName ="";
		
		workerName = nameWorker;
		
	System.out.println("My Name is " + workerName);
	
		
		
	}
	
 public static void worker(String nameWorker ,int workingHour ){
		
		String workerName ="";
		
		workerName = nameWorker;
		
	System.out.println("My Name is " + workerName);
	
	if(workingHour>12){
		
		System.out.println("I am Over Work");
	}
	
	else{
		
		System.out.println("I am okay");
	}
	
		
		
	}

}


Check out Interface in Java Here

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − 2 =