Interfaces: - Inside - Outside - An Interface Is A List of Methods That Must Be
Interfaces: - Inside - Outside - An Interface Is A List of Methods That Must Be
INSIDE
OUTSIDE
An interface is a list of methods that must be
defined by any class which implements that
interface.
It may also define constants (public static final).
Uses
A very common use of interfaces is for
listeners.
A listener is an object from a class that
implements the required methods for that
interface.
You can create anonymous inner listeners, or
implement the required interface in any class.
Interfaces are also used extensively in the
data structures (Java Collections) package.
Interfaces simulate multiple inheritance.
Declaring an interface
For simple programs you are more
likely to use an interface than define
it. Here is what the
java.awt.event.ActionListener
interface definition looks something
like the following.
public interface ActionListener { public
void actionPerformed(ActionEvent e); }
Example
interface mathType
{
public mathType add ();
public mathType sub();
public mathType mul ();
public mathType div ();
}
Class math extends mathType
{
public mathType add()
{
}
}
An Interface is defined by the name interface
Tagging Interfaces
Java defines a few interfaces that are just used as a boolean
property of a class, but don't actually require the implemenation
of any methods. These are called tagging interfaces.
Tagging interfaces are an abuse of the interface principle, but
Java makes use of object-oriented features to solve all problems if
possible, rather than invent a more appropriate feature.
Common tagging interfaces that you might see are:
Cloneable Implementing this interface indicates that the class has
overridden Object's clone() method. But there is no check that this is
true, and because subclasses inherit interfaces, it will look like all
subclasses have defined clone() although that is not necessarily true
Serializable This tagging interface indicates that a class can serialized that an object of this class can be written out and read back in using ???