Created
March 16, 2013 14:53
-
-
Save noelmarkham/5176718 to your computer and use it in GitHub Desktop.
Testing variance in Java's generics. Why on earth doesn't this compile? In IntelliJ, getClass on line 13 errors as 'cannot resolve symbol'
Javac doesn't produce a logical error.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GenericVariance { | |
class A {} | |
class B extends A {} | |
@Test | |
public void checkTypes() { | |
assertTrue(new B() instanceof A); | |
final List<A> listOfA = new ArrayList<>(); | |
final List<B> listOfB = new ArrayList<>(); | |
assertFalse(listOfB instanceof listOfA.getClass()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment