Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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?

like image 749
Abhishek Choudhary Avatar asked Mar 20 '12 15:03

Abhishek Choudhary


2 Answers

getClass().getSuperclass() should do.

like image 123
Bozho Avatar answered Oct 18 '22 10:10

Bozho


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.

like image 41
sepp2k Avatar answered Oct 18 '22 09:10

sepp2k