Java Class <T> static method forName () IncompatibleClassChangeError

private static Imported getRightInstance (String s) throws Exception {Class c = Class.forName (s) .asSubclass (Importable.class); Imported i = c.newInstance (); return i; }

which I can also write

private static Importable getRightInstance(String s) throws Exception {
   Class<? extends Importable> c = (Class<? extends Importable>)Class.forName(s);
   Importable i = c.newInstance();
   return i;
}

or

private static Importable getRightInstance(String s) throws Exception {
   Class<?> c = Class.forName(s);
   Importable i = (Importable)c.newInstance();
   return i;
}

where Importable is the interface and s is the string representing the implementation class. Well, in any case, it gives the following:

Exception in thread "main" java.lang.IncompatibleClassChangeError: class C1 has
interface Importable as super class

Here is the last fragment of the stack trace:

 at java.lang.Class.forName(Class.java:169)
 at Importer.getRightImportable(Importer.java:33)
 at Importer.importAll(Importer.java:44)
 at Test.main(Test.java:16)

Now, class C1 actually imports Importable, and I don't completely understand why it complains.

Thanks in advance.

+3
source share
1 answer

IncompatibleClassChangeError , - , . , , Importable , C1 , . JVM extends SomeClass implements SomeInterface, C1 Importable (, , extends implements.

+1

Source: https://habr.com/ru/post/1746781/


All Articles