Created
October 6, 2015 10:30
-
-
Save smamran/65fdd78f7d2439d9fb97 to your computer and use it in GitHub Desktop.
Instanceof Operator Typecasting use
This file contains hidden or 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
| package slidenerd.javaoop; | |
| /** | |
| * Created by Microsoft on 10/6/2015. | |
| */ | |
| public class InstanceOfOperator { | |
| public static void main(String[] args) { | |
| Dog dog = new Dog(); | |
| System.out.println(dog instanceof Animal); // true | |
| dog = null; // as dog refers to null | |
| System.out.printf("Now dog instance of Dog = %b", dog instanceof Dog); // false | |
| System.out.println(); | |
| System.out.printf("Now dog instance of Animal = %b", dog instanceof Animal); // false | |
| System.out.println(); | |
| Animal animal = new Animal(); | |
| System.out.printf("Now animal instance of Dog = %b", animal instanceof Dog); // false | |
| System.out.println(); | |
| Animal an = new Dog(); | |
| // instanceof operator checking | |
| if (an instanceof Dog) { | |
| Dog dg = (Dog) an; | |
| System.out.println("Typecasting possible"); | |
| System.out.println(dg); // java.lang.ClassCastException at runtime | |
| } else { | |
| System.out.println("Typecasting cann't possible"); // output | |
| } | |
| } | |
| } | |
| class Animal { | |
| } | |
| class Dog extends Animal { | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment