Is it possible to create an instance of a derived class in abstract ancestor class using reflection Lets say:
abstract class Base {
public Base createInstan
Proving it works is easy:
abstract class Base {
public Base createInstance() throws Exception {
return getClass().newInstance();
}
}
public class Derived extends Base {
public static void main(String[] args) throws Exception {
System.out.println(new Derived().createInstance().getClass());
}
}
prints
class test.Derived
You should ask yourself twice why you need it and whether it is really a good approach for your problem. If you need cloning, consider the clone
mechanism, which does basically the same thing.