OnJava8-Examples/typeinfo/toys/GenericToyTest.java
2020-10-07 13:35:40 -06:00

23 lines
708 B
Java

// typeinfo/toys/GenericToyTest.java
// (c)2020 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Testing class Class
// {java typeinfo.toys.GenericToyTest}
package typeinfo.toys;
public class GenericToyTest {
public static void
main(String[] args) throws Exception {
Class<FancyToy> ftClass = FancyToy.class;
// Produces exact type:
FancyToy fancyToy = ftClass.newInstance();
Class<? super FancyToy> up =
ftClass.getSuperclass();
// This won't compile:
// Class<Toy> up2 = ftClass.getSuperclass();
// Only produces Object:
Object obj = up.newInstance();
}
}