I am confused with this code, which is supposed not to work (I guess), but it seems to work with no errors.
When I put <T>
next to class Person, the wildcard in Arraylist don't behave as it is defined, in the code below, the wildcard should only work for super
Person class, but with the <T>
next to the class Person, it accept all kind for types (in this example it's String
). And also for this to work, Human when defined should not have the type specified. Here's the code:
import java.util.ArrayList;
public class Person {
public static void main(String[] args) {
Human h = new Human();
h.al(new ArrayList<String>()); // this should give an error no ?
}
}
class Human<T>{ //Human is a generic class, but above I created an instance without specifying the T
public void al(ArrayList<? super Person> a){ //the type passed should be a super of Person
}
}
Thank you :)