i've got a Problem with java and generic classes.
Given the following code
public class A {
public void n() {
System.out.println("In A");
}
}
public class B extends A {
@Override
public void n() {
System.out.println("In B");
super.n();
}
}
public class C {
public A m_a;
public <T extends A> C( Class<T> a ) {
try {
m_a = a.newInstance();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void print(){ m_a.n(); };
}
i try to instantiate an Object of Class C as following
C c = new C( B.class );
but get the following error:
java.lang.InstantiationException: testdbvsfile.Main$B
at java.lang.Class.newInstance(Unknown Source)
at testdbvsfile.Main$C.<init>(Main.java:63)
at testdbvsfile.Main.main(Main.java:76)
Caused by: java.lang.NoSuchMethodException: testdbvsfile.Main$B.<init>()
at java.lang.Class.getConstructor0(Unknown Source)<br>
... 3 more
How can i make it work?
Thanks for help.
btw: I'm using jre1.8.0_65