/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Box<T> {
private T element;
public T getElement() {
return element;
}
public void setElement(T element) {
this.element = element;
}
}
class Ideone
{
{
List<Box> l = new ArrayList<>(); //Just List of Box with no specific type
Box<String> box1 = new Box<>();
box1.setElement("aa");
Box<Integer> box2 = new Box<>();
box2.setElement(10);
l.add(box1);
l.add(box2);
//Case 1
Box<Integer> b1 = l.get(0);
System.
out.
println(b1.
getElement()); //why no error
//Case 2
Box<String> b2 = l.get(1);
System.
out.
println(b2.
getElement()); //throws ClassCastException }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgQm94PFQ+IHsKICAgIHByaXZhdGUgVCBlbGVtZW50OwoKICAgIHB1YmxpYyBUIGdldEVsZW1lbnQoKSB7CiAgICAgICAgcmV0dXJuIGVsZW1lbnQ7CiAgICB9CgogICAgcHVibGljIHZvaWQgc2V0RWxlbWVudChUIGVsZW1lbnQpIHsKICAgICAgICB0aGlzLmVsZW1lbnQgPSBlbGVtZW50OwogICAgfQp9CgpjbGFzcyBJZGVvbmUKewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4gKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBqYXZhLmxhbmcuRXhjZXB0aW9uCgl7CiAgICAgICAgTGlzdDxCb3g+IGwgPSBuZXcgQXJyYXlMaXN0PD4oKTsgLy9KdXN0IExpc3Qgb2YgQm94IHdpdGggbm8gc3BlY2lmaWMgdHlwZQogICAgICAgIEJveDxTdHJpbmc+IGJveDEgPSBuZX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