Not possible.
Java is a language of strong types. This means that the compiler checks if the methods and attributes you are accessing exist and are compatible with code usage. The only way to do this is if the compiler has access to the classes being used and the code is importing those dependencies correctly.
If it were possible to use weak typing as in scripting languages the cast would have no advantage whatsoever, just call any method on the generic object and that's it.
Anyway, what you're trying to do is basically force Java to create a type dynamically without however presenting a definition of that type.
To access methods and attributes of dynamically loaded classes, you must use reflection. There are already some answers here, including mine that teaches you to do this, but basically you can use the getMethod
method of the class and retrieve a reference to the method, then you use the invoke
method to execute the method.
See an example from documentation :
public class InvokeMain {
public static void main(String... args) {
try {
Class<?> c = Class.forName(args[0]);
Class[] argTypes = new Class[] { String[].class };
Method main = c.getDeclaredMethod("main", argTypes);
String[] mainArgs = Arrays.copyOfRange(args, 1, args.length);
System.out.format("invoking %s.main()%n", c.getName());
main.invoke(null, (Object)mainArgs);
} catch (ClassNotFoundException x) {
x.printStackTrace();
} catch (NoSuchMethodException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
} catch (InvocationTargetException x) {
x.printStackTrace();
}
}
}