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

后端 未结 3 914
[愿得一人]
[愿得一人] 2020-12-07 02:07

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 it

3条回答
  •  长情又很酷
    2020-12-07 02:24

    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.

提交回复
热议问题