Yeah, I know. Long title of question… So I have class name in string. I’m dynamically creating object of that class in this way:
String className = "com.package.MyClass";
Class c = Class.forName(className);
Object obj = c.newInstance();
How I can dynamically convert that obj to MyClass object? I can’t write this way:
MyClass mobj = (MyClass)obj;
…because className can be different.
you don’t, declare an interface that declares the methods you would like to call:
then you use
If
MyClassis not under your control then you can’t make it implement some interface, and the other option is to rely on reflection (see this tutorial).