Method Overloading in Java
Method Overloading in Java
- Two or more methods with the same name under same class, it is called method overloading.
- It example for static polymorphism.
- Binding of the method call, is done during compile time and hence it is called static bindingorearly binding.
- The overloaded methods can differ by the number of arguments or argument type or sequence of the arguments.
Example:
package Mypkg; public class MethodOverLoadex { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Entered MethodOverLoadex Main() "); myOverloading obj=new myOverloading(); obj.getCal();// method calling obj.getCal(10,20); System.out.println("Exited MethodOverLoadex Main() "); } } class myOverloading { int num1,num2;// actual paramters void getCal(int a, int b)// Two input { System.out.println("Entered myOverloading getCal(a,b) "); System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); num1=a; num2=b; System.out.println("multiplication "+ " => "+ num1*num2); System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); System.out.println("Exited myOverloading getCal(a,b) "); } void getCal()// Taking zero input { System.out.println("Entered myOverloading getCal() "); System.out.println("*************************"); num1=100; num2=200; System.out.println("Addition "+ " => " + (num1+num2)); System.out.println("*************************"); System.out.println("Exited myOverloading getCal() "); } }
Output:
Entered MethodOverLoadex Main() Entered myOverloading getCal() ************************* Addition => 300 ************************* Exited myOverloading getCal() Entered myOverloading getCal(a,b) &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& multiplication => 200 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Exited myOverloading getCal(a,b) Exited MethodOverLoadex Main()
Click Here-> Get Java Training with Real-time Projects