The problem of Java generics is the same as the code in the book, but the result is different?

parent: Fruit
subclass: Apple

List < Fruit > fruitList = new ArrayList < Fruit > ();

fruitList.add (new Apple ()); is OK, but I don"t want Apple to fit in

so create a function

public static < T > void add (List < T > list,T item) {

    list.add(item);
}


result add (fruitList,new Apple ()); can still be put in

however, "java programming ideas" should not be put into

.

I"m confused. Has the mechanism in the book been removed?

Oct.19,2021

found the same question outside the wall. The author made a mistake here.

Menu