-5

/** * Created by zhangzhongzheng on 2016/10/15. */

public class ExtendsTest {

static Dog d = new Dog();

public static void main(String[] args) {
    Animal a = d;
    System.out.println(a instanceof Animal);//true
    System.out.println(a instanceof Dog);//true

    System.out.println(d instanceof Animal);//true
    System.out.println(d instanceof Dog);//true
}

static class Animal {

}

static class Dog extends Animal {

}

}

why all true??????

J.Long
  • 1
  • 1
  • 3
    There are three here that should be slap-in-the-face obvious and the other is too if you take a little time to investigate [`instanceof`](http://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for) or just consider the name slowly. – ChiefTwoPencils Oct 15 '16 at 09:09

2 Answers2

0

Here Animal is a Parent and Dog is a child.

Parent class reference can hold child class object.

Animal animal = new Dog();

So all above conditions are true.

Anil M
  • 3
  • 1
  • 7
0

In keeping with the concept of type,instanceos says,"Are you this class,or a class derived from this class?"-------<>

J.Long
  • 1
  • 1