java - Generics vs. Method Overloading -
i have 3 methods:
//1 -- check 1 item public static <t> void containsatleast(string message, t expecteditem, collection<? extends t> found) { if (!found.contains(expecteditem)) assert.fail("..."); } //2 -- check several items public static <t> void containsatleast(string message, collection<? extends t> expecteditems, collection<t> found) { (t expteteditem : expecteditems) containsatleast(message, expteteditem, found); } //3 -- check several items, without message parameter public static <t> void containsatleast(collection<? extends t> expecteditems, collection<? extends t> found) { containsatleast(null, expecteditems, found); }
i expect method //3
invoke //2
not, invoke method //1
. there mistake in expect?
*i use sdk 1.7.0_25 , eclipse 4.3*
your second method expects generic type of expecteditems
(? extends t
) subtype of generic type of found
(t
).
in third method, there no subtype relationship between 2 generic types. both extend t
siblings example.
so second method can't called.
example: imagine call third method types:
containsatleast(collection<integer> e, collection<string> f)
so t
in third method object
. , first method called t = object
too.
Comments
Post a Comment