This code works, and I dont understand how the list is able to add a raw type box, I thought type erasure will set the type to their indicated bounds. note: Paper class doesn't extends Bakery class.
EDIT: Is my understanding correct? the compiler typecasts the raw box type so that it can be added to the list? does this works only at compile time? so if I tried to get the value at runtime it will throw an exception?
class Violator {
public static List<Box<? extends Bakery>> defraud() {
List<Box<? extends Bakery>> list = new ArrayList<>();
Paper paper = new Paper();
Box box = new Box<>();
box.put(paper);
list.add(box);
return list;
}
}
class Box<T> {
void put(T item) { /* implementation omitted */ }
T get() { /* implementation omitted */ }
}