OnJava8-Examples/reflection/toys/GenericToyTest.java
Bruce Eckel ede3954d86 March 2021 Book Update
See notes in "Foreword to the Leanpub Edition"
2021-03-04 16:15:04 -07:00

23 lines
732 B
Java

// reflection/toys/GenericToyTest.java
// (c)2021 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 reflection.toys.GenericToyTest}
package reflection.toys;
public class GenericToyTest {
public static void
main(String[] args) throws Exception {
Class<FancyToy> ftc = FancyToy.class;
// Produces exact type:
FancyToy fancyToy =
ftc.getConstructor().newInstance();
Class<? super FancyToy> up = ftc.getSuperclass();
// This won't compile:
// Class<Toy> up2 = ftc.getSuperclass();
// Only produces Object:
Object obj = up.getConstructor().newInstance();
}
}