Overriding member variables in Java ( Variable Hiding)

前端 未结 12 956
小蘑菇
小蘑菇 2020-11-22 03:07

I am studying overriding member functions in JAVA and thought about experimenting with overriding member variables.

So, I defined classes

public clas         


        
12条回答
  •  礼貌的吻别
    2020-11-22 03:56

    There is no polymorphism for fields in Java.

    Variables decision happens at a compile time so always Base Class variables (not child’s inherited variables) will be accessed.

    So whenever upcasting happens always remember

    1) Base Class variables will be accessed.

    2) Sub Class methods(overridden methods if overriding happened else inherited methods as it is from parent) will be called.

提交回复
热议问题