RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
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:

  1. 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).

  2. You need a callback mechanism (function pointers in Java) for such things as a JDBC connection interface, Swing event listeners, or MOM.
Rajasekaran Kandhasamy
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date