How does instance variable invocation work when you do A thing = new B(); where B is a subclass of A?

前端 未结 6 750
予麋鹿
予麋鹿 2021-01-27 17:54

This is probably answered somewhere, but I have no idea what to search for. Imagine you have the following...

The superclass, Animal.java

public class An         


        
6条回答
  •  闹比i
    闹比i (楼主)
    2021-01-27 18:42

    This is because Java talks only about method overriding. Member variables can only be shadowed in child class. So when you say c.noise it actually refers to the string variable in parent class as the c if reference type Animal.

提交回复
热议问题