Overriding member variables in Java ( Variable Hiding)

前端 未结 12 866
小蘑菇
小蘑菇 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:40

    As per the Java specifications, the instance variables are not overridden from a super class by a sub class when it is extended.

    Hence the variable in the sub class only can be seen as one sharing the same name.

    Also when the constructor of A is called during the instance creation of B the variable (intVal) is initialized and hence the output.

    0 讨论(0)
  • 2020-11-22 03:43

    Variables are resolved compile-time, methods run-time. The aRef is of type A, therefore aRef.Intvalue is compile-time resolved to 1.

    0 讨论(0)
  • 2020-11-22 03:44

    Well, I hope u got the answer. If not, you can try seeing in the debug mode. the subclass B has access to both the intVal. They are not polymorphic hence they are not overriden.

    If you use B's reference you will get B's intVal. If you use A's reference , you will get A's intVal. It's that simple.

    0 讨论(0)
  • 2020-11-22 03:44

    As Many users have already pointed out, this is not polymorphism. Polymorphism only applies to methods(functions).

    Now as to why the value of the intVal of class A is printed, this happens because as you can see the reference aRef is of type A.

    I can see why you are confused by it. By the same procedure you have accessed the overridden methods for ex. the method identifyClass() but the not the variables which directly proves the first line that I have written .

    Now in order to access the variable you can do ((Superclass)c).var

    Note here that the Superclass can be many levels up for example A<-B<-C. That is C extends B and B extends A. If you wanted the value of var of A then you could have done ((A)c).var .

    EDIT: as one of the users have pointed out this 'trick' does not apply to static methods, because they are static.

    0 讨论(0)
  • 2020-11-22 03:46

    Variables are not polymorphic in Java; they do not override one another.

    0 讨论(0)
  • 2020-11-22 03:47

    From JLS Java SE 7 Edition §15.11.1:

    This lack of dynamic lookup for field accesses allows programs to be run efficiently with straightforward implementations. The power of late binding and overriding is available, but only when instance methods are used.

    Answers from Oliver Charlesworth and Marko Topolnik are correct, I would like to elaborate a little bit more on the why part of the question:

    In Java class members are accessed according the type of the reference and not the type of the actual object. For the same reason, if you had a someOtherMethodInB() in class B, you wouldn't be able to access it from aRef after aRef = b is run. Identifiers (ie class, variable, etc names) are resolved at compile time and thus the compiler relies on the reference type to do this.

    Now in your example, when running System.out.println(aRef.intVal); it prints the value of intVal defined in A because this is the type of the reference you use to access it. The compiler sees that aRef is of type A and that's the intVal it will access. Don't forget that you have both fields in the instances of B. JLS also has an example similar to yours, "15.11.1-1. Static Binding for Field Access" if you want to take a look.

    But why do methods behave differently? The answer is that for methods, Java uses late binding. That means that at compile time, it finds the most suitable method to search for during the runtime. The search involves the case of the method being overridden in some class.

    0 讨论(0)
提交回复
热议问题