Java polymorphism creating a subclass object using its superclass variable

前端 未结 3 1845
猫巷女王i
猫巷女王i 2021-01-02 09:48

So I am a student and in the process of learning Java. There is one concept that I am having a difficult time grasping and am hoping that someone could shed some light on t

3条回答
  •  -上瘾入骨i
    2021-01-02 10:15

    1. This is correct, even though the reference is of type Animal, all method calls will resolve to the definition in Lizard if present, otherwise the version in the next immediate parent will be called and so on.

    2. a is just a reference and the actual object is of type Lizard. So, the constructors in Lizard class will be called. They in turn can call the constructors in super classes using super().

提交回复
热议问题