I learned that I can use the real type of a Object to define which Method is used, such like this:
[...]
Object foo = new String("hello");
[...]
bla(foo);
void bla(String x){
}
void bla(Integer x){
}
Now it should take the bla(String x)
method, but instead I get compiler error. I know why: because the type of foo
is currently Object
. I'm not sure if I do it currently right, but I understood it that way, that Java will choose the method by the real type (or specific type), so if I downcast it to Object
, it will choose the String
instead, if no Object
Method is specified.
Or is the only way to determinate the type by if(foo instanceof xxx)
in a method void bla(Object x)?
P.S.: dont get me wrong on this: I dont mean that I can overload methods, I mean that I want to choose the method based on the real type (not on the defined one)!