Lets say I have a parent class and a child class which extends my parent and I have the following code.
public class SomeClass {
private Parent myParent;
public SomeClass(Parent myParent) {
this.myParent = myParent;
}
}
Why is this allowed
Class<SomeClass> clazz = SomeClass.class;
SomeClass someClass = clazz.getConstructor(Parent.class).getInstance(Child);
and this isnt't
Class<SomeClass> clazz = SomeClass.class;
SomeClass someClass = clazz.getConstructor(Child.class).getInstance(Child);
The second one throws an NoSuchMethodeException. Why isn't there dynamic binding in this case but when using the normal Constructor dynamic binding works just fine? And is there a way to workaround this problem?
Edit:
I'm trying to load jar files at runtime. At this moment I have the classes I need, loaded with an URLClassLoader . Next I want to create a new instance of one of the loaded classes. To create the instance i call urlClassLoader.loadClass(nameOfClass).getConstructor(parameterType).newInstance(initArguments);
The parameterTypewould be child.class in this case.