Oct 5, 2009
A Guide to Determine When You Need a Java Interface
You should create a Java interface if your needs meet either of the following scenarios:
- When you need plug-in capabilities to go with an interface option. As an example, consider a Java-based car racing video game. As shipped, it supports BMW and Audi cars, but I want third-party car providers to be able to create cars for this game. Because each car type should be able to react differently to user input and conditions, I created one interface with leftKey(), rightKey(), upKey(), and downKey() methods; every car should implement these methods.
Now, for example, Mercedes could create a car that understands this interface implementation and provide it to me. When a race starts with this new car, my host application can execute without knowing the underlying object type (third-party Mercedes).
- You need a callback mechanism (function pointers in Java) for such things as a JDBC connection interface, Swing event listeners, or MOM.