MyInterface.java
publc interface MyInterface{
void print();
}
Abc.java
public Class Abc implements MyInterface{
public void print(){
System.out.print("Inside Abc");
}
}
Xyz.java
public Class Xyz implements MyInterface{
public void print(){
System.out.print("Inside Xyz");
}
}
Main.java
public Class Main{
public static void main(String arg[]){
String classPath="Abc"; // this String will get assign @ runtime.
Class<?> s = Class.forName(classPath);
}
}
Here inside main method classPath is "Abc", so i'm expecting Abc Instance. The classsPath string will be Abc or Xyz or any Class Name that implements MyInterface.So depending the classPath String i want the instance of that class. like if ClassPath is "Abc" then Abc Class instance, ClassPath is "Xyz" then Xyz Class instance. How can i achieve this dynamically.