why super.getClass() in a subclass returns subclass name

后端 未结 3 1325
礼貌的吻别
礼貌的吻别 2020-12-20 14:30

I am inside a subclass and when I am trying to find the name of super class, I tried super.getClass() , but it is returning me the name of the subclass only. Why?

相关标签:
3条回答
  • 2020-12-20 14:33

    If you override a method from your superclass (or your superclass's superclass etc.), super.theMethod() will invoke the original method instead of the one you overrode it with. If you did not actual override theMethod, super.theMethod() will act exactly like theMethod().

    In this case I assume you did not override getClass() (in fact I know you didn't because it's final), so super.getClass() acts exactly like getClass(), i.e. either way the getClass method of the Object class is called.

    0 讨论(0)
  • 2020-12-20 14:41

    This is because you are creating object of derived class and not super class.. you may try this

    this.getClass().getSuperClass();
    
    0 讨论(0)
  • 2020-12-20 14:59

    getClass().getSuperclass() should do.

    0 讨论(0)
提交回复
热议问题