Overriding member variables in Java ( Variable Hiding)

前端 未结 12 1119
小蘑菇
小蘑菇 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 04:06

    I hope this can help:

    public class B extends A {
    //  public int intVal = 2;
    
        public B() {
            super();
            super.intVal = 2;
        }
    
        public void identifyClass() {
            System.out.println("I am class B");
        }
    }
    

    So overriding variable of base class is not possible, but base class variable value can be set (changed) from constructor of inherited class.

提交回复
热议问题