Get Concrete Class name from Abstract Class

JavaAbstract Class

Java Problem Overview


In Java, inside an abstract class can I get the instance of the concrete class that extends it?

Java Solutions


Solution 1 - Java

Yes, you can do this by calling this.getClass(). This will give you the Class instance for the runtime type of this.

If you just want the name of the class, you could use this.getClass().getName().

Lastly, there are also this.getClass().getSimpleName() and this.getClass().getCanonicalName(). I use the former all the time to print readable class names to log files and the like.

Solution 2 - Java

Thread.currentThread().getStackTrace()[2].getClassName()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionblong824View Question on Stackoverflow
Solution 1 - JavaNPEView Answer on Stackoverflow
Solution 2 - JavaLeonid IvankinView Answer on Stackoverflow