Skip to content

Instantly share code, notes, and snippets.

@Tanner
Created November 18, 2011 03:12
Show Gist options
  • Select an option

  • Save Tanner/1375475 to your computer and use it in GitHub Desktop.

Select an option

Save Tanner/1375475 to your computer and use it in GitHub Desktop.
CS 1331 Dynamic-Type Casts
public class Test {
public static void main(String[] args) {
Test test = new Test();
}
public Test() {
Corgi a = (Corgi)(new TeaCupCorgi()); // Compiles and Runs
Corgi b = (TeaCupCorgi)(new Corgi()); // Compiles, but doesn't Run
Corgi c = new Corgi(); // Compiles (this is the "new Corgi()" portion above on line 8)
Corgi d = (TeaCupCorgi)c; // Compiles, but doesn't Run (making line 10 into line 8)
}
private class Animal { }
private class Dog extends Animal { }
private class Corgi extends Dog { }
private class TeaCupCorgi extends Corgi { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment