You are on page 1of 8

INTERFACES

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

Relatively large applications use


interfaces, easier to modify, extend
and integrate new features
One implementation of an given
interface, a slightly different behavior
will require a new class to one of the
existing interfaces.
Adapt a class from different hierarchy
to work in an existing application.

Abstract Classes And


Interfaces
Java interface is a definition of a class type
without concrete implementation.
Java interfaces are implicitly abstract, they
cannot be directly instantiated.
The class must implement all the methods
described in the interface.
All interfaces consists of one or more
method signatures.
The subclasses fulfill to confirm the type.
usually

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.

Classes versus Interfaces


Classes are used to represent something
that has attributes (variables, fields) and
capabilities/responsibilities (methods,
functions). Interfaces are only about
capabilities.
For example, Human(class). Interfaces are
the ability of being a electrician, plumber,
etc..,
You can implement many interfaces, but
be only one class.

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 ???

You might also like