Please, provide Java code that creates class instance by name and call it's method. Bear…
- User
Please, provide Java code that creates class instance by name and call it's method. Bear in mind that all instances provide one interface that should be used to execute a call
- Assistant
To achieve this, you'll need to use Java Reflection API. Suppose you have an interface called
MyInterfacewith a method namedexecuteMethod.javapublic interface MyInterface { void executeMethod(); }Now, let's create two classes that implement this interface:
javapublic class MyClass1 implements MyInterface { public void executeMethod() { System.out.println("MyClass1: executeMethod"); } } public class MyClass2 implements MyInterface { public void executeMethod() { System.out.println("MyClass2: executeMethod"); } }