0

I'm calling ChildTest class methods with ChildTest reference only i.e.

ChildTest child = new ChildTest(); // not dynamic dispatching. 

My question is why child.foo() output is Test foo called 10

class Test {
    int x = 10;

    public void foo() {
        System.out.println("Test foo called " + x);

    }

    public static void main(String args[]) {
        ChildTest child = new ChildTest();

        System.out.println(child.x);
        child.foo();
        child.bar();
    }
}

class ChildTest extends Test {
    int x = 20;

    public void bar() {
        System.out.println("ChildTest Bar called " + x);
    }
}
MarmiK
  • 5,639
  • 6
  • 40
  • 49
ManishS
  • 627
  • 2
  • 11
  • 21

4 Answers4

0

Variables cannot be overridden in Java, so what you have done is that you have hidden the original x variable.

The fact that you have added a variable x in your ChildTest class and given it the value 20 does not affect the variable x from your Test class, which will still be 10.

The method foo() still resides in the Test class, which means that it will use the x variable declared in that class. If you want to see a different output for a call from a ChildTest reference, you can add a getX() method and override it:

class Test {
    public int getX() {
        return 10;
    }
    public void foo() {
        System.out.println("Test foo called " + getX());
    }
    ...

class ChildTest extends Test {
    @Override
    public int getX() {
        return 20;
    }
    ...
Keppil
  • 45,603
  • 8
  • 97
  • 119
0

The line int x = 20; in ChildTest declares a separate variable also called x. An instance of ChildTest then has two unrelated variables named x, one from Test with value 10 and one from ChildTest with value 20. Code in class Test can only see the variable it declared itself, which is why it sees the value 10.

If you want ChildTest to modify the value rather than creating a separate variable, add a constructor (or initializer block) and set the variable there without redeclaring it:

class ChildTest extends Test {
    ChildTest() {
        x = 20;
    }

    public void bar() {
        System.out.println("ChildTest Bar called " + x);
    }
}
Boann
  • 48,794
  • 16
  • 117
  • 146
0

The reason is that there is no way to override variables in Java, what you are doing is hiding it.

Then, since main method is in the context of Test class the value of the variable is 10, in order to get 20 you shall be in the context of the ChildTest class or set the value of the x through a initialization block, constructor, or anything like this.

Here is more information: Is there a way to override class variables in Java?

Community
  • 1
  • 1
Rafael
  • 2,521
  • 2
  • 33
  • 59
0

child is the object refrence of ChildTest,when you call foo() method like child.foo(), it looks out for the method in ChildTest class,but there is no such method in it,however when it finds that it is inheriting a class that is Test class which is having this foo() method,Then it executes it and uses the instance variables of the Test class.

nobalG
  • 4,544
  • 3
  • 34
  • 72