Method Overloading in Java
Hello, friends, Hitesh is here; today we learn about
method overloading in java. In a simple and easy way; If a class have the same method with different parameter is know as method overloading.
Achieve Method Overloading
You can achieve method overloading by following below two steps;
- By changing parameters
- By changing the data type
Method Overloading by changing the number of parameters
class Addition
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(int a,int b,int c)
{
System.out.println(a+b+c);
}
public static void main(String args[])
{
Addition obj=new Addition();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
Method Overloading by Changing data type
class Addition
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(double a,double b)
{
System.out.println(a+b);
}
public static void main(String args[])
{
Addition obj=new Addition();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}
No comments:
Post a Comment