In Java super.getClass() prints “Child” not “Parent” - why is that?

早过忘川 提交于 2019-11-27 19:30:59

问题


In Java classes and objects, we use "this" keyword to reference to the current object within the class. In some sense, I believe "this" actually returns the object of itself.

Example for this:

class Lion
{
    public void Test()
    {
        System.out.println(this);  //prints itself (A Lion object)
    }
}

In the scenario of a superclass and subclass. I thought that "super" keyword would return the object of the superclass. However it seems that I got it wrong this time:

Example:

class Parent
{
    public Parent(){
    }
}

class Child extends Parent
{
    public Child(){
        System.out.println(super.getClass()); //returns Child. Why?
    }
}

My Quesiton: In the above example, I was expecting the compiler to print out class Parent, however it prints out class Child. Why is this so? What super actually returns?


回答1:


A method call using super just ignores any overrides in the current class. For example:

class Parent {
    @Override public String toString() {
        return "Parent";
    }
}

class Child extends Parent {
    @Override public String toString() {
        return "Child";
    }

    public void callToString() {
        System.out.println(toString()); // "Child"
        System.out.println(super.toString()); // "Parent"
    }
}

In the case of a call to getClass(), that's a method which returns the class it's called on, and can't be overridden - so while I can see why you'd possibly expect it to return Parent.class, it's still using the same implementation as normal, returning Child. (If you actually want the parent class, you should look at the Class API.)

This is often used as part of an override, in fact. For example:

@Override public void validate() {
    // Allow the parent class to validate first...
    super.validate();
    // ... then perform child-specific validation
    if (someChildField == 0) {
        throw new SomeValidationException("...");
    }
}



回答2:


The getClass() method returns the class of the object. Super & this reference the same object. So it is not because you reference your object with super that suddenly the object changes class, it remains an instance of the class that was used to instantiate it, hence the getClass() will always return the same Class whether you call it from this or from super.

Super is meant to be used to reference an implementation of a method as defined in a super class, it is typically used in those methods that are being overridden in a sub class to call the original behaviour.




回答3:


The super keyword will call an overridden method in the parent. Child did not override that method. So it will faithfully return the correct class which is Child. You do not get an instance of the Parent class this way.



来源:https://stackoverflow.com/questions/28002433/in-java-super-getclass-prints-child-not-parent-why-is-that

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