Java: Reflection against casting when you know superclass
- by Ema
I don't know exactly how to define my doubt so please be patient if the question has already been asked.
Let's say I have to dinamically instantiate an object. This object will surely be instance of a subclass of a known, immutable class A. I can obtain dinamically the specific implementation class.
Would it be better to use reflection exactly as if I didn't know anything about the target class, or would it be preferrable/possible to do something like:
A obj = (Class.forName("com.package.Sub-A")) new A();
where Sub-A extends A ?
The purpose would be to avoid reflection overhead times...
Thank you in advance.