How does inheritance of instance fields work in this particular code?

后端 未结 10 1654
我寻月下人不归
我寻月下人不归 2020-12-24 11:11
class A
{
    int a = 2, b = 3;
    public void display()
    {
        int c = a + b;
        System.out.println(c);
    }
}
class B extends A
{
    int a = 5, b =          


        
相关标签:
10条回答
  • 2020-12-24 11:26

    Java doesn't have anything like variable overriding. Thus, when the method display() is invoked, it accesses the variables inside the parent class 'A' and not the variables inside the subclass 'B'.

    It can be explained with the same reason of why you can't print a variable declared in a subclass (and not in superclass) inside superclass method. The superclass method simply doesn't have access to the subclass variables.

    However, you'll be able to print 5,11 if you have accessor methods to the fields in both the classes and you use those accessor methods to get the values instead of directly accessing using variable names. (even if the display() method is present only in superclass). This is because the overridden accessor methods are invoked (in second case) which return the values from the subclass.

    0 讨论(0)
  • 2020-12-24 11:29

    There isn's anything called variable overriding. That is why you are getting the same result in both the cases.

    0 讨论(0)
  • 2020-12-24 11:41

    Class B declares variables in B scope, public void display() is part of A class and knows only about its own scope variables.

    0 讨论(0)
  • 2020-12-24 11:41

    It's the inheritance functaionality which gives the output 5,5.

    0 讨论(0)
  • 2020-12-24 11:43

    why does the output comes 5,5?

    Because A.display() only knows about the fields A.a and A.b. Those are the only fields that any code in A knows about. It looks like you expect the declarations in B to "override" the existing field declarations. They don't. They declare new fields which hide the existing fields. Variables don't behave virtually in the way that methods do - the concept of overriding a variable simply doesn't exist. From the JLS section 8.3:

    If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.

    You can get the effect you want by changing B so that its constructor changes the values of the existing fields that it inherits from A instead:

    class B extends A {
        B() {
            a = 5;
            b = 6;
        }
    }
    

    Note that these are not variable declarations. They're just assignments. Of course in most code (well, most code I've seen anyway) the fields in A would be private, so couldn't be accessed from B, but this is just example for the purpose of explaining the language behaviour.

    0 讨论(0)
  • 2020-12-24 11:43

    The reason is that Java uses the concept of lexical scope for variable resolution.

    Fundamentally, there are two possible options to resolve free variables in a function ('free' means not local and not bound to function parameters):

    1) against the environment in which the function is declared

    2) against the environment in which the function is executed (called)

    Java goes the first way, so free variables in methods are resolved [statically, during compilation] against their lexical scope (environment), which includes:

    • method parameters and local method variables
    • field declarations in the class containing method declaration
    • public field declarations in parent class
    • and so on, up the chain of inheritance

    You would see this behaviour implemented in most programming languages, because it is transparent to developer and helps prevent errors with shadowing of variables.

    This is opposite to the way methods work in Java:

    class A {
        public void foo() {
            boo();
        }
        public void boo() {
            System.out.println("A");
        }
    }
    class B extends A {
        @Override
        public void boo() {
            System.out.println("B");
        }
    }
    class Main {
        public static void main(String[] args) {
            B b = new B();
            b.foo(); // outputs "B"
        }
    }
    

    This is called dynamic dispatch: method call is resolved dynamically in runtime against the actual object, on which it is called.

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