I have two overloaded function as follows:
Public Void Test(String S)
{
//Something
}
Public Void Test(Object S)
{
//Something
}
Now if I call Test with null value, which function will get called and why ?
Test(null);
Loading
Jaish MathewsPosted May 26, 2010, 11:37 AM
Direct answer is that your code won't compile. It will generate error like "Test" method atlease need 1 parameter.
Now it call it by assing null, then which parameter is more specific root level, that method will be called. Here method with "String" is more specific. So that will be called. Method with "Object" parameter cvan store any type, so will get 2nd priority only. If you add below 2 additional overload methods , by commenting string parameter method, you will get better idea.
public class A
{
/*
public void Test(String S)
{
//Something
}
*/
public void Test(Object S)
{
//Something
}
public void Test(Array S)
{
//Something
}
public void Test(string[] S)
{
//Something
}
}
public class B
{
public static void Main(String[] s)
{
A objA = new A();
objA.Test(null);
}
One method has "Array" and another has normal array of "string[]". 2nd type is more root level and this willl be called.
CrishPosted May 26, 2010, 10:48 AM
As per my understanding Overloaded function means Function name is same always but its parameter type is changed or parameters length is more than other overloaded function.
in this scenarios below function is called
Public Void Test(String S)
{
//Something
}
Dipankar SanaPosted May 26, 2010, 8:03 AM
Public Class A()
{
Public Void Test(String S)
{
//Something
}
Public Void Test(Object S)
{
//Something
}
}
Public Class B()
{
public static void Main(String[] s)
{
A objA = new A();
objA.Test();
}
Which function will get called in this scenario ?
}
Jaish MathewsPosted May 26, 2010, 7:49 AM