daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Object i = Integer.valueOf(42);
  2. String s = (String)i;            // ClassCastException thrown here.
  3.    
  4. class A {...}
  5. class B extends A {...}
  6. class C extends A {...}
  7.    
  8. Animal a = new Dog();
  9. Dog d = (Dog) a; // no problem, the type animal can be casted to a dog, because its a dog
  10. Cat c = (Dog) a; // raises class cast exception, you cant cast a dog to a cat
  11.    
  12. public void manipulate(Object o) {
  13.      Dog d = (Dog) o;
  14.  }
  15.    
  16. Animal a = new Animal();
  17.  manipulate(a);
  18.    
  19. Dog d;
  20.  if(o instanceof Dog) {
  21.      d = (Dog) o;
  22.  } else {
  23.      //what you need to do if not
  24.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top