Casting a class using reflection in java
问题 I have the following classes. abstract class A{} class B extends A{} class C extends A{} I need to create an object like this A a = new B(); I get the class name of the subclass at runtime. So, I need to use reflection to create an object. I have done this. Class<?> klass = Class.forName(className); Now I am not able to cast this class to the subclass. I want to achieve A a = klass.newInstance(); // returns Object I want the object to be casted into the subclass (either B or C , decided on