Java “trick”, redefining daughter class member

后端 未结 6 997
青春惊慌失措
青春惊慌失措 2021-02-19 06:03

I\'m training for a Java exam, and I\'ve come across something I don\'t understand in last year subject. Here is the code

class Mother {
    int var = 2;

    in         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 06:46

    m = new Daughter();
    

    Though you have created a daughter object, you are referring that object with Mother m reference. So, any call using m will call the Mother class members, not daughter's

提交回复
热议问题