Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Object i = Integer.valueOf(42);
- String s = (String)i; // ClassCastException thrown here.
- class A {...}
- class B extends A {...}
- class C extends A {...}
- Animal a = new Dog();
- Dog d = (Dog) a; // no problem, the type animal can be casted to a dog, because its a dog
- Cat c = (Dog) a; // raises class cast exception, you cant cast a dog to a cat
- public void manipulate(Object o) {
- Dog d = (Dog) o;
- }
- Animal a = new Animal();
- manipulate(a);
- Dog d;
- if(o instanceof Dog) {
- d = (Dog) o;
- } else {
- //what you need to do if not
- }
Add Comment
Please, Sign In to add comment