Monday, February 9, 2009

Method Overloading

Method Overloading:
Two methods having the same name are not allowed in the case of C-language, for every data type even though the functionality in the same; we should maintain different method names. It increases the complexity of the programming. But in java, two methods having the same name is allowed irrespective of parameters. We can maintain the same method name for similar type of functionality. It simplifies the programming. Such types of methods are called Overloaded methods.
• Two methods are said to be overloaded if and only of they have the same method name, but different parameter list(at least order).
• The signatures of the two overloaded methods must be different.
Example:
public void m1(){}
private int m1(int i){}

• Here m1() & m1(int i) are overloaded. We never consider return type, access modifier and throws class in overloading.
Example:
class Sample
{
public void m1()
{
System.out.println(“no arg”);
}
public void m1(int i)
{
System.out.println(“ int arg”);
}
public void m1(double d)
{
System.out.println(“ double arg”);
}
public static void main(String a[])
{
Sample s=new Sample();
s.m1();
s.m1(10);
s.m1(10.5);
}
}

• In the case of overloading which overloaded method must be executed is decided method must be executed is decided by compiler only based on reference type.
• Ie., Overloaded method resolution is the duty of compiler only. Hence Overloading is the best
ex. of static polymorphism and some times also known as Early Binding.

s.m1(10L);//double arg
s.m1(‘a’);//int arg

i.e., Automatic Promotion in Overloading:
• If we are calling a method m1() by passing char as arg on the sample reference then the compiler will check in the Sample class for m1(), which can take char as the arg.
• If it finds that method, it will execute at run time.If there is no such method the compile promotes that char arg to the int arg and checks for m1(int) method.
• If there is no such method, the compiler will check for m1(long), fallowed by m1(float),fallowed by m1(double). Still if the compiler won’t find any such method then only compiler will raised CTE saying ‘cannot resolve the symbol m1(char).
CASE 1:
class Sample
{
public void m1(int i,float f)
{
System.out.println(“int,float”);
}
public void m1(float i,int f)
{
System.out.println(“float,int”);
}
public static void main(String a[])
{
Sample s=new Sample();
s.m1(10,10.0f);//int,float
s.m1(10.0f,10);//float,int
s.m1(10,10);//invalid-> CTE:reference m1 is ambiguous
}
}
CASE 2:
class Sample
{
public void m1(String s)
{
System.out.println(“String version”);
}

public void m1(Object o)
{
System.out.println(“Object version”);
}
public void m1(double d)
{
System.out.println(“double version”);
}
public static void main(String a[])
{
Sample s=new Sample();
s.m1(“viswan”);// String version
s.m1(new Object());// Object version
s.m1(null);// String version
}
}
If there is Double (wrapper class) then CTE: reference to m1 is ambiguous.
CASE 3:
class Animal
{
}
class Monkey extends Animal
{
}
public class OLStaticBinding {

public void m1(Animal a)
{
System.out.println("Animal version");
}
public void m1(Monkey m)
{
System.out.println("Monkey version");
}
public static void main(String a[])
{
OLStaticBinding s=new OLStaticBinding();
Animal a1 =new Animal();
s.m1(a1); // Animal version
Monkey m =new Monkey();
s.m1(m); // Monkey version
Animal a2 =new Monkey();
s.m1(a2); // Animal version

}

}
}
In the case of overloading the method resolution performed by the compiler is based on the reference type.

0 comments:

Post a Comment