Java OCA OCP Practice Question 2332

Question

Which method declarations cannot be inserted independently at (2) to overload the method at (1)?

public class Main {
   static <T> void overloadMe(List<T> s1, List<T> s2) { } // (1)
   // (2) INSERT DECLARATION HERE.
}

Select the two correct answers.

(a) static <T> void overloadMe(Collection<T> s1, List<T> s2) { }
(b) static <T> void overloadMe(List<T> s1, List<? extends T> s2) { }
(c) static <T> void overloadMe(List<T> s1, Collection<? super T> s2) { }
(d) static <T> void overloadMe(Collection<T> s1, Collection<? super T> s2) { }
(e) static <T> void overloadMe(Collection<T> s1, List<? super T> s2) { }
(f) static <T> void overloadMe(List<? extends T> s1, List<? super T> s2) { }


(b) and (f)

Note

After erasure, the method at (1) has the signature overloadMe(List,List).

Since all methods are declared void, they must differ in their parameter list after erasure in order to be overloaded with the method at (1).

All methods have different parameter lists from that of the method at (1), except for the declarations (b) and (f).

In other words, all methods have signatures that are not override-equivalent to the signature of the method at (1), except for (b) and (f).

If one considers the declarations (a) and (e) on their own, these two methods have the same signature overloadMe(Collection,List) after erasure and, therefore, would not be overloaded.




PreviousNext

Related