I have a List of classes that implement the IApplication
interface:
public interface IApplication {
public void start();
}
public class Configurator implements IApplication {
public void start() {...};
}
public static void main(String[] args) {
List<IApplication> apps = new ArrayList<IApplication>();
apps.add(new Configurator());
apps.get(0).start(); //ClassCastException
}
When the last line in my Main function is executed, I get a ClassCastException
:
java.lang.ClassCastException: Configurator cannot be cast to IApplication
Even if I instantiate a Configurator object as an IApplication, I get the the following error:
IApplication app = new Configurator();
app.start();
java.lang.IncompatibleClassChangeError: Class Configurator does not implement the requested interface IApplication
Should this not be allowed in Java? Could it be an issue where my Configuration class is not getting updated correctly with the implementation?ClassCastException from a List of objects which implement a common interface