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
- Numbers of Different parameter in the arguments
- Different of Data Type in Arguments
- Different sequence of data type of Arguments
Things to take note about Overloading in Java
- Method Overloading only perform inside a Class.
- Return Type can be the Same or different .
- Method Parameter should be different
Class overloadMain
- First I create 2 Method with the same Method Name “public static void worker”
- The Difference between the First Method and the second method are , different Parameter type.
- 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