mercredi 16 mars 2022

How to Get Child Class Name of a Parent Interface in Java

I want to get the child class name which is implemented by an interface. For example

public interface A
public class B implements A
public class C implements A
...

In my code I have declared the interface A and have set the value to one of these classes. So what I have is this:

A a = object.getA();

Now I want to get the current child class name, so B, C or whatever, but I don't want to do it with instanceof because I have many child classes which makes the code unnessesary long. I am basically looking for this but getChild() doesn't exist of course.

a.getChild().getClass().getName()

Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire