In Java, inside an abstract class can I get the instance of the concrete class that extends it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, you can do this by calling
this.getClass(). This will give you theClassinstance 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.