Method Overloading is a type of polymorphism.
Using this we can have two different method having the same name in a single class.
Method have the same name but different argument through which they are recognize.
Argument have
1.Different number of argument.
2.Different type of datatype.
3.Different sequence of datatype.
1.different number of argument
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
2.different type of dataype
class DisplayOverloading2
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(int c)
{
System.out.println(c );
}
}
class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5);
}
}
3.different sequence of argument
class DisplayOverloading3
{
public void disp(char c, int num)
{
System.out.println("Im the first definition of method disp");
}
public void disp(int num, char c)
{
System.out.println("Im the second definition of method disp" );
}
}
class Sample3
{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
obj.disp(52, 'y');
}
}
0 Comment(s)