I have child classes, each of which carries a different type of value along with other members. There can be LongObject, IntObject, StringObject, etc.
I will be given a value that can be long, int, string, etc., and I need to create LongObject, IntObject, StringObject, etc. respectively.
Would it be faster to overload the method as shown below (a), or just use elseif as shown below (b)?
This may not be a noticeable difference in performance. Maybe overloaded methods are implemented in the same way as if / else. I dont know.
I also hear some of you talking just to check it out. Of course I have to. I would also like to know how this type of overload is handled under the hood, if anyone knows.
Please let me know what you think.
Thanks JBU
a)
BaseObject getObject(long l)
{
return new LongObject(l);
}
BaseObject getObject(int i)
{
return new IntObject(i);
}
BaseObject getObject(String s)
{
return new StringObject(s);
}
...
b)
BaseObject getObject(Object x)
{
if(value is a long)
return new LongObject((Long)x);
else if(value is an int)
return new IntObject((Int)x);
else if(value is a String)
return new StringObject((String)x);
...
}
edit: I think I did not completely add all the details, some of you caught it. For both options, I still have to get the object / value, and from the value determine what type it is. So I still need to do if / else to even use the overloaded methods.