jazz pop
jazz pop

Reputation: 5

Variable Hiding confusion?

So this is basically my code

abstract class B
{
    int x = 3;
    B()
    {
        x+=2;
        System.out.print("-x" + x + "\n"); // print -x5
        x++; // 5 then 6

    }
    abstract int calculate();
    abstract int calculate(int i);
}
class A extends B
{
    int x = 2;
    A()
    {
       System.out.print("-x" + calculate(2)+"\n");  
    }
    @Override
    int calculate()
    {

        return x;
    }
    @Override
    int calculate(int i)
    {
        return(calculate()+i);
    }
}
public class Test2 extends A
{
    Test2()
    {
       x+=3;
    }
    @Override
    int calculate()
    {
        return x + 6;
    }
    public static void main(String[] args) {
        Test2 sub = new Test2();
        System.out.print("-x" + sub.calculate()+"\n");
    }
}

My problem here is after digging up about variable hiding I learned that if a instance variable is of the same name in both parent class and child class then the childclass hides the instance variable of the parent class. Also I have the prior knowledge that variables cannot be overridden when child class inherits parent class.

So now coming to the problem, in the code when A extends to B, why does the print statement inside constructor A() gives a value -x10? shouldn't it be -x8?? I dont understand how the variable is being changed here. I am new to java so any kind of knowledge will be greatly appreciated. :)

Ok so I have done some debugging and found that the calculate(void) method in class A returns 8. But how is that possible shouldn't it return 6? Please help!

Upvotes: 0

Views: 66

Answers (2)

steven7mwesigwa
steven7mwesigwa

Reputation: 6710

It is returning 8 because: The line you called System.out.print("-x" + sub.calculate()+"\n"); in class A calls

    @Override
    int calculate()
    {
        return x + 6;
    }

in class A still, which is incrementing the instance variable int x = 2 in class A . this variable overwrote the one in class B
Hence 2+6 = 8

Upvotes: 0

fishinear
fishinear

Reputation: 6336

The reason it prints -x10 is because A::calculate(2) calls the Test2::calculate(), which uses A::x to do the calculation.

The sequence of calls that happens is the following:

Test2() {
  A()
    B() {
      B::x = 3
      B::x += 2
      System.out.print("-x" + x + "\n"); // print -x5
      B::x++ // B::x is now 6
    }

    A::x = 2

    System.out.print("-x" + calculate(2)+"\n")
    A::calculate(2) {
        return(calculate()+2);
        Test2::calculate() {
            return A::x + 6;     // A::x is 2 here, so returns 8
        }
    } // returns calculate()+2, so returns 10
  }

  A::x += 3
}

I hope this is just code to test things out, because you should never allow this to happen in real code. You should never allow a method of a subclass to be called from the constructor of a base class, because the subclass is not initialised at that time. The Java compiler does its best to prevent that, but sometimes it does not detect it.

Upvotes: 1

Related Questions