When a class contains more than one method with the same method name but different argument types, then it is called Overloading.
Methods are said to be Overloaded methods.
Also, know as Compile time binding or Early binding.
The compiler takes care of overloaded methods and decides according to the signature which way is to be invoked.
Eg: class Display
{
public void show( int b) //1
{System.out.println(b);}
public void show(String s) //2
{System.out.println(s);}
public static void main(String args[ ])
{
Display d = new Display();
d.show(2);//1 method invoked
d.show("Swati");//2 method invoked
d.show( 10.5f)/*in 3 invocation
1 method gets invoked though show(float ) not present*/
}
}
1 &2 are overloaded methods in class Display.During a method call compiler first searches for exactly matched method calls in class.3 invocation compiler actually searches for show(float f) but it is not found.So it again searches for the method having argument that can be promoted into float and encounters 1 method.As we all know int can be converted into float.This is called Automatic argument promotion.
Output of program
2
Swati
10.5