Friday, 5 September 2014

instanceOf comparison


class Tree {

}

class Pine extends Tree{

}

class Oak extends Tree{

}

class TestTree{
    public static void main (String args[]){
        Tree t = new Pine();
        System.out.println(t instanceof Pine);  // returns true
        System.out.println(t instanceof Tree);  // returns true
        System.out.println(t instanceof Oak);   // returns false
        System.out.println(t instanceof Object);   // returns true

        Tree tree = new Tree();
        System.out.println(tree instanceof Pine);  // returns false
        System.out.println(tree instanceof Tree);  // returns true
        System.out.println(tree instanceof Oak);   // returns false
        System.out.println(tree instanceof Object);   // returns true
    }
}

No comments:

Post a Comment