OnJava8-Examples/generics/ErasureAndInheritance.java
2015-05-29 14:18:51 -07:00

27 lines
671 B
Java

//: generics/ErasureAndInheritance.java
// ©2015 MindView LLC: see Copyright.txt
class GenericBase<T> {
private T element;
public void set(T arg) { element = arg; }
public T get() { return element; }
}
class Derived1<T> extends GenericBase<T> {}
class Derived2 extends GenericBase {} // No warning
// class Derived3 extends GenericBase<?> {}
// Strange error:
// unexpected type found : ?
// required: class or interface without bounds
public class ErasureAndInheritance {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Derived2 d2 = new Derived2();
Object obj = d2.get();
d2.set(obj); // Warning here!
}
} ///:~