3 ms·
It's not about methods with different signatures being being different methods, but about when they are bound. If a class has these methods: void print(Objec
by 10ren 18y ago
It's not about methods with different signatures being being different methods, but about when they are bound. If a class has these methods:
void print(Object obj) {..}
void print(String str) {..}
and we make the following invocation - which one gets called? The static, compile-time type of the invocation is Object and the dynamic, runtime class of the invocation is String. Which one will select the method?
Object a = "hello";
print(a);
In Java, the first method gets called (the one taking an Object argument). Because the compile time type of this invocation is Object, it is bound to the first method, regardless of the runtime class of a. Even if a is a String. It's the compile time type that selects the method, not the runtime class. To select the other method, we need to change the compile time type:
print((String)a);