Why does the line marked with //1 print 57 instead of 39?

戏子无情 提交于 2020-03-28 06:55:50

问题


class X {
    protected int v = 0;

    public X() {
        v += 10;
    }

    public void proc(X p) {
        System.out.println(43);
    }
}

class Y extends X {
    public Y() {
        v += 5;
    }

    public void proc(X p) {
        System.out.println(57);
    }

    public int getV() {
        return v;
    }
}

class Z extends Y {
    public Z() {
        v += 9;
    }

    public void proc(Z p) {
        System.out.println(39);
    }
}

class Main {
    public static void main(String[] args) {
        X x = new Z();
        Y y = new Z();
        Z z = new Z();
        x.proc(z);// 1
        System.out.println(y.getV());
    }
}

From what I can understand,the method proc() is called on an object of type X that "holds" a type Z and at runtime JVM checks the object's type and overrides the method with the proc() method from Y.But the method parameter is of type Z,why doesn't it call the overloaded method from the Z class?


回答1:


It happens because you don't override the method 'proc' in Z class. When you overriding the method, you can't use argument, which has a child class of original argument class. If you add @Override on Z.proc(Z p), your code will be not compiled.

Let's imagine that it is possible, then you can use some method from Z class during executing Z.proc(Z p).

class Z extends Y {
    public Z() {
        v += 9;
    }

    public void proc(Z p) {
        someActions();
        System.out.println(39);
    }

    private void someActions() {
        System.out.println("Some actions");
    }

}

Now when you executing

X x = new Z();
x.proc(new X());

What should happens? There is no 'someActions' method in the X class. How it should works? That's why Z.proc(Z p) doesn't override X.proc(X p). Class Z, has two different methods: Z.proc(Z p) and Y.proc(X p).

When you calling

X x = new Z();
x.proc(new Z());

JVM looks for closest overrided or original method with signature 'proc(X)' to Z class (because X class has 'proc(X)' method) finds it in Y class and executing Y.proc(x p). That's why you see '57' in output.




回答2:


Because you're passing an X to proc, so the proc for class Y prevails.

If you passed an actual Z declared as a Z, it would have printed 39.

Z x = new Z();
X y = new Z();
Z z = new Z();


z.proc(x); // prints 39
z.proc(y); // prints 57



回答3:


Since Y is the closest to Z in the hierarchy, you will always get 57 by passing an instance of Z referenced by Y or above in the hierarchy to the overridden proc e.g.

class Main {
    public static void main(String[] args) {
        X x = new Z();
        Y y = new Z();
        Z z = new Z();
        x.proc(z);
        y.proc(z);
        z.proc(z);

        x.proc(y);
        y.proc(y);
        z.proc(y);

        x.proc(x);
        y.proc(x);
        z.proc(x);
    }
}

Output:

57
57
39
57
57
57
57
57
57

As you can see from the output, the only occasion when you get 39 is when you call proc on the reference of Z.




回答4:


Just to elaborate over Federico answer. The "x" object is, in fact, a "X" variable that point to a "Z" instance in memory. "Z" instance has only two methods:

void proc(Z p)
void proc(X p)

But the runtime only is aware of the second method that is implemented at "X" class and its overridden by the method implemented in "Y" class. So, when you call:

x.proc(z)

The runtime only knows about of this second method and call it, executing the overridden one.



来源:https://stackoverflow.com/questions/60269396/why-does-the-line-marked-with-1-print-57-instead-of-39

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