I came acress this one:
"Given:
Code: Select all
class Base{
public <T extends Number, Z extends Number> Map<T, Z> getMap(T t, Z z)
{
return new HashMap<T, Z>();
}
}
class Derived extends Base{
//public <T, Z> TreeMap<T, Z> getMap(T t, Z z) { return new TreeMap<T, Z>(); }; //1
//public Map<Number, Number> getMap(Number t, Number z) { return new TreeMap<Number, Number>(); }; //2
//public Map<Integer, Integer> getMap(Number t, Number z) { return new HashMap<Integer, Integer>(); }; //3
}
Correct answer is:
//1 correctly overloads while //2 and //3 correctly override the method in Base.
The rules for multiple type parameters are same as the rules for a single type parameter. You have to apply the same rules for both the type parameters separately. For example, we know that A<S> is a valid subtype of A<? extends T> (where S is a subtype of T). Therefore, Map<Integer, Integer> is a valid subtype of Map<T extends Number, Z extends Number>. The bounds defined by <T extends Number> and <T> are different. Therefore, the parameter list of //1 i.e. getMap(T t, Z z) is different from the parameter list of the Base class's method. Thus, it is a valid overload.
The way I see it. //1 and the baseClass have the same signature ( getMap(T t, Z z)), so one would be an override, no? Am I missing something?