It's not necessity at all, in most cases. If you really want to just
define the methods in the class and not "implement the interface", you
can certainly do that. But one big advantage of using an interface is
the "is a" test.
For example, one very commonly used interface is ActionListener. It
defines only one method, actionPerformed(ActionEvent). If your class
includes "implements ActionListener", then an instance of your class is
allowed to be passed anywhere that a parameter requires an object of
type ActionListener. This includes addActionListener() and
removeActionListener() methods.
Another widespread use is demonstrated with the List interface. Many
common structures, such as ArrayList, LinkedList and Vector, implement
this interface. This means you could create a variable of type List and
use only those methods defined in the List interface for accessing its
contents. It also means that you could decide to use an ArrayList
instead of a Vector without making changes to any exposed methods in
your class, if it's available to other classes to call.
Those are just a couple examples of its benefits that come to mind.
Answered by
Madhurima
, an ibibo Master,
at
4:32 PM on August 15, 2008