This is an extension of this previous question about parameterized methods. I am reading the same book. After the example in the previous question, the author improves the
BinaryTree<T extends Comparable<? super T>>
class yet again (and he really does this time around) by adding the following constructor
public <E extends T> BinaryTree(E[] items) {
for(E item : items) {
add(item);
}
In the spirit of the previous question I tried this constructor instead:
public BinaryTree(T[] items) {
for(T item : items) {
add(item);
}
}
and the example code does not compile with my constructor:
public static void main(String[] args) {
Manager[] managers = { new Manager("Jane", 1), new Manager("Joe", 3), new Manager("Freda", 3), new Manager("Bert", 2), new Manager("Ann", 2), new Manager("Dave", 2) };
BinaryTree<Person> people = new BinaryTree<>(managers);
}
What is this difference between changing the add() method in the previous question and changing this constructor? Why can't I pass a subtype of T in my constructor?