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

后端 未结 10 1655
我寻月下人不归
我寻月下人不归 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:48

    When doing this:

    class B extends A
    {
        int a = 5, b = 6;
    }
    

    you are not redefining a and b, you're creating new variables with the same names. So you end up with four variables( A.a, A.b, B.a, B.b).

    When you call display() and calculate the value of c, A.a and A.b will be used, not B.a and B.b

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

    When you compile your code it pretty much becomes like:

    class A extends java.lang.Object
    {
        int a=2,b=3;
        public void display()
        {
            int c=a+b;
            System.out.println(c);
        }
    }
    class B extends A
    {
        int a = 5, b = 6;
        public void display()
        {
          super(); //When you call y.display() then this statement executes.
        }
    }
    class Tester
    {
        public static void main(String arr[])
        {
            A x = new A();
            B y = new B();
            x.display();
            y.display();
        }
    }
    

    And hence, when super calls, the method of class A is being called.

    Now go to method of class A. Here int c = a + b; means c = this.a + this.b; which is 2 + 3.

    And the result is 5.

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

    Why does the output come out as 5,5? And not 5,11?

    Whenever we have same instance variables (applicable to class variable as well) in a class hierarchy, the nearest declaration of the variable get the precedence. And in this case, nearest declaration of a and b from display () method is A’s. So class B’s instance variables go hidden. Hence in both cases, 5 gets printed.

    How would the y.display() method work?

    Another alternative is to have getter in both classes to get value of a and b.

        class A
    {
        int a = 2, b = 3;
        public int getA() {
            return a;
        }
    
        public int getB() {
            return b;
        }
        public void display()
        {
            int c = getA() + getB();
            System.out.println(c);
        }
    }
    class B extends A
    {
        int a = 5, b = 6;
        public int getA() {
            return a;
        }
    
        public int getB() {
            return b;
        }
    }
    class Tester
    {
        public static void main(String arr[])
        {
            A x = new A();
            B y = new B();
            x.display();
            y.display();
        }
    }
    

    Prints

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

    In class A you declare fields a and b. The method display uses these fields. In class B you declare NEW fields of the same name. You're actually hiding the old fields not "overriding" them. To assign different values to the same fields use a constructor:

    class A {
        A(int a, int b) {
            this.a = a;
            this.b = b;
        }
    
        A() {
            this(2, 3);
        }
    
        int a,b;
    
        public void display() {
            int c=a+b;
            System.out.println(c);
        }
    }
    
    class B extends A {
        B() {
            super(5, 6);
        }
    }
    
    0 讨论(0)
提交回复
热议问题