Method Overloading Compile Time Polymorphism 


When a class contain multiple method with same name but different number of argument then is called method overloading .overloading is also known as the compile time polymorphism .

Syntax of method overloading


Example - same method name but different number of argument 
class Addition

void add (int a){}
void add (int a , int b){}
 

Example - Same method name and same number of argument but different data types.
 class Addition
{  
void add (int a){}
void add (char c{}
 

Way to overload the method 
There are 2 way to overload a method in java programming.
1) By changing the number of parameter.
2) By changing the data type of parameter.

Method overloading example - By Changing the number of parameter.


class Addition

void add (int a)
{
System.out.println(a);
}
void add (int a , int b)
{
System.out.println(a+b);
}
 
class Test 
{  
public static void main(String args[])
{  
Addition s=new Addition(12);
Addition s=new Addition(12,10);
}
}

Output : 
12
22

Method Overloading Example : By changing the data type of parameter.

class Addition

void add (int a)
{
System.out.println(a);
}
void add (char ch)
{
System.out.println(ch);
}
 
class Test 
{  
public static void main(String args[])
{  
Addition s=new Addition(12);
Addition s=new Addition('S');
}
}

Output :
12
S

Hope !! The above lesson of Method overloading in java helpful for you..

Technology Team, 

UPCS 
UP Consultancy Services 


Tags : Method overloading in java , example of method overloading , way of method overloading , Syntax of method overloading