I apologize if this question has been asked already. I searched the questions and could not find an answer.
In Java, inside an abstract class can I get the instance of the concrete class that extends it?
If so, can I see a code example?
I apologize if this question has been asked already. I searched the questions and could not find an answer.
In Java, inside an abstract class can I get the instance of the concrete class that extends it?
If so, can I see a code example?
Yes, you can do this by calling
this.getClass()
. This will give you theClass
instance for the runtime type ofthis
.If you just want the name of the class, you could use
this.getClass().getName()
.Lastly, there are also
this.getClass().getSimpleName()
andthis.getClass().getCanonicalName()
. I use the former all the time to print readable class names to log files and the like.