Overriding a super class's instance variables

后端 未结 9 1932
暗喜
暗喜 2020-11-30 07:47

Why are we not able to override an instance variable of a super class in a subclass?

9条回答
  •  旧时难觅i
    2020-11-30 08:17

    Do you mean with overriding you want to change the datatype for example?

    What do you do with this expression

    public class A {
       protected int mIndex;
    
       public void counter(){
          mIndex++;
       }
    
    }
    
    public class B extends A {
       protected String mIndex; // Or what you mean with overloading
    }
    

    How do you want to change the mIndex++ expression without operator overloading or something like this.

提交回复
热议问题