OnJava8-Examples/containers/ApplesAndOrangesWithoutGenerics.java

37 lines
1000 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// containers/ApplesAndOrangesWithoutGenerics.java
2015-06-15 17:47:35 -07:00
// <20>2015 MindView LLC: see Copyright.txt
// Simple container use (suppressing compiler warnings)
// {ThrowsException}
import java.util.*;
class Apple {
private static long counter;
private final long id = counter++;
public long id() { return id; }
}
class Orange {}
public class ApplesAndOrangesWithoutGenerics {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList apples = new ArrayList();
for(int i = 0; i < 3; i++)
apples.add(new Apple());
// Not prevented from adding an Orange to apples:
apples.add(new Orange());
for(Object apple : apples) {
((Apple) apple).id();
// Orange is detected only at run time
}
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
___[ Error Output ]___
Exception in thread "main" java.lang.ClassCastException:
Orange cannot be cast to Apple
at ApplesAndOrangesWithoutGenerics.main(ApplesAndOr
angesWithoutGenerics.java:23)
___[ Exception is Expected ]___
2015-09-07 11:44:36 -06:00
*/