Can someone please guide me on this. I have a class loader which I could load a class using Java reflection. However, is there anyway I can cast my object to an interface? I understand that there is a ServiceLoader but I read it is highly not recommended.
//returns a class which implements IBorrowable
public static IBorrowable getBorrowable1()
{
IBorrowable a; //an interface
try
{
ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
a = (IBorrowable) myClassLoader.loadClass("entityclasses.Books");
}
catch (Exception e ){
System.out.println("error");
}
return null;
}
newInstance()on the returned class before casting it. This of course assumes you have a default constructor on the actual class.