Java Inheritance - this keyword

时光毁灭记忆、已成空白 提交于 2019-12-07 06:58:09

问题


I searched online for similar question, but could not find it. So, posting here.

In the following program why the value of 'i' is printed as 100?

AFAIK 'this' refers to the current object; which in this case is 'TestChild' and the class name is also correctly printed. But why the value of the instance variable is not 200?

public class TestParentChild {
    public static void main(String[] args) {
        new TestChild().printName();
    }
}

class TestChild extends TestParent{
    public int i = 200;
}

class TestParent{
    public int i = 100;
    public void printName(){
        System.err.println(this.getClass().getName());
        System.err.println(this.i); //Shouldn't this print 200
    }
}

And moreover the output of the following is as i expected. The child class method is invoked, when i call "this.test()" from Parent class.

public class TestParentChild {
    public static void main(String[] args) {
        new TestChild().printName();
    }
}

class TestChild extends TestParent{
    public int i = 200;
    public void test(){
        System.err.println("Child Class : "+i);
    }

}

class TestParent{
    public int i = 100;
    public void printName(){
        System.err.println(this.getClass().getName());
        System.err.println(this.i); //Shouldn't this print 200
        this.test();
    }
    public void test(){
        System.err.println("Parent Class : "+i);
    }
}

回答1:


Java does not have virtual fields, so the i field in printName always refers to TestParent.i and not any descendant child.

Polymorphism through inheritance in Java only happens with methods, so if you want the behaviour you're describing then you'd want this:

class TestChild extends TestParent{

    private int i = 200;

    @Override
    public int getI() { return this.i; }
}

class TestParent{

    private int i = 100;

    public int getI() { return this.i; }

    public void printName(){
        System.err.println( this.getClass().getName() );
        System.err.println( this.getI() ); // this will print 200
    }
}



回答2:


Because fields in Java aren't inherited. Using your declarations, you have effectively declared two different fields named i, and instances of TestChild will have both. When TestParent is compiled, references to i in its methods will always refer to TestParent.i.




回答3:


There is no way to override a class variable.

You do not override class variables in Java, instead you hide them. Overriding is for instance methods and hiding is different from overriding.

In the example you've given, by declaring the class variable with the name 'i' in class TestChild you hide the class variable it would have inherited from its superclass TestParent with the same name 'i'. Hiding a variable in this way does not affect the value of the class variable 'i' in the superclass TestParent

To get the desired behavior, you can just override the getI() method

class TestChild extends TestParent{

    private int i = 200;

    @Override
    public int getI() {
         return this.i;
    }
}


来源:https://stackoverflow.com/questions/14097666/java-inheritance-this-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!