OnJava8-Examples/generics/ErasureAndInheritance.java

29 lines
775 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// generics/ErasureAndInheritance.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
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:
2016-01-25 18:05:55 -08:00
// unexpected type
2015-06-15 17:47:35 -07:00
// 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!
}
2015-09-07 11:44:36 -06:00
}