Accessing constructor from abstract base class with reflection
- by craesh
Hi!
I'm playing around with Java's Reflection. I have an abstract class Base with a constructor.
abstract class Base {
public Base( String foo ) {
// do some magic
}
}
I have some further classes extending Base. They don't contain much logic. I want to instantiate them with Base's constructor, without having to write some proxy contructors in those derived classes. And of course, I want to instantiate those derived classes with Reflection. Say:
Class cls = SomeDerivedClass.class;
Constructor constr;
constr = cls.getConstructor( new Class[] { String.class } ); // will return null
Class clsBase = Base.class;
constr = clsBase.getConstructor( new Class[] { String.class } ); // ok
Base obj = (Base) constr.newInstance( new Object[] { "foo" } ); // will throw InstantiationException because it belongs to an abstract class
Any ideas, how I can instantiate a derived class with Base's constructor? Or must I declare those dumb proxy constructors?