When you have different classes that implements certain interface you may deal with them in polymorphic way.
First, lets change your code to make it according to java code conventions:
public interface Interface {
void action();
}
public class ClassOne implements Interface {
@Override
public void action(){
System.out.println("class one action");
}
}
public class ClassTwo implements Interface {
@Override
public void action(){
System.out.println("class two action");
}
}
public class ClassThree implements Interface {
@Override
public void action(){
System.out.println("class three action");
}
}
After that create list and add new instances to it and call method action()
for each element in list, you don't need to know actually what class instances presents in list, you only need to know that all of them implements certain interface.
List<Interface> list = new ArrayList<>();
list.add(new ClassOne());
list.add(new ClassTwo());
list.add(new ClassThree());
for(Interface instance : list) {
instance.action();
}
output will be:
class one action
class two action
class three action
P.S. you may even write this in one line with java 8/9
java 8
Arrays.asList(new ClassOne(), new ClassTwo(), new ClassThree()).forEach(System.out::println);
java 9
List.of(new ClassOne(), new ClassTwo(), new ClassThree()).forEach(System.out::println);