Method overloading:-
The process of extending existed method functionality up to
some new functionality is called as method Overloading.
To perform method overloading in a java applications, we
have to declare more then one method with the same name and with the different
parameter list .
In method overloading all the method must have different
method signature that is same method name and different parameter list.
Ex code 1:-
class A
{
void
add(int i,int j)
{
int
k=i+j;
System.out.println(k);
}
void
add(float f1,float f2)
{
float
f=f1+f2;
System.out.println(f);
}
void
add(String str1,String str2)
{
String
str=str1+str2;
System.out.println(str);
}
}
class Test
{
public
static void main(String[] args)
{
A
a=new A();
a.add(10,20);
a.add(22.22f,33.33f);
a.add("abc","def");
}
}
Output:-
30
55.55
Abcdef
Ex code2:-
class Employee
{
public
void gen_Salary(int basic, float hk, int ta, float pf)
{
float
sal=(basic+((basic*hk)/100)+ta-((basic*pf)/100));
System.out.println("Salary:"+sal);
}
public
void gen_Salary(int basic, float hk, int ta, float pf, int bonus)
{
float
sal=(basic+((basic*hk)/100)+ta-((basic*pf)/100)+bonus);
System.out.println("Salary:"+sal);
}
}
class Test
{
public
static void main(String[] args)
{
Employee emp=new Employee();
emp.gen_Salary(20000,25.0f,5000,12.5f);
emp.gen_Salary(20000,25.0f,5000,12.5f,10000);
}
}
Output:-
Salary:27500.00
Salary:37500.00
No comments:
Post a Comment