This document discusses Java interfaces. It defines an interface as a collection of constants and abstract methods. Interfaces have public visibility by default for methods. A class implements an interface by stating it in the class header and defining all of the interface's abstract methods. Interfaces allow for polymorphism through reference variables that can refer to objects of different classes that all implement the same interface. Interface hierarchies can also exist where a child interface inherits methods from a parent interface.
3. JAVA INTERFACE
3
◉ A J ava interface is a collection of constants and
abstract methods
◼ abstract method: a method header without a method
body; we declare an abstract method using the
modifier a b s t ra c t
◼ since all methods in an interface are abstract, the
a b s t ra c t modifier is usually left off
◉ Methods in an interface have public visibility by
default
4. INTERFACE: SYNTAX
interfaceisa reserved word
public interface Doable
{
public static f i n a l String NAME;
public void
doThis();
public i n t doThat();
public void doThis2 ( f l o a t value, char ch);
public boolean doTheOther ( i n t num);
}
A semicolon immediately
followseach method
header
No method in an
interfacehas a definition
4
5. IMPLEMENTING AN INTERFACE
5
◉ A class formally implements an interface by
◼ stating so in the class header in the implements
clause
◼ a class can implement multiple interfaces: the
interfaces are listed in the implements clause,
separated by commas
◉ If a class asserts that it implements an interface, it
must define all methods in the interface or the
compiler will produce errors
6. IMPLEMENTING INTERFACES
public class Something implements
Doable
{
public void doThis ( )
{
//
whateve
r
}
public void doThat ( )
{
// whatever
}
implements is
a reserved
word
Each method
listed
in Doable
is
given a
definition
// etc.
}
public c l a s s ManyThings implements Doable,
AnotherDoable 6
9. INTERFACES: EXAMPLES FROM
JAVA STANDARD CLASS LIBRARY
9
◉ The Java Standard Class library defines many
interfaces:
◼ the I t e r a t o r interface contains methods that
allow the user to move through a collection of
objects easily
□ h a sN ex t (), n e x t ( ) , remove()
◼ the Comparable interface contains an abstract
method called compareTo, which is used to compare
two objects
i f (obj1.compareTo(obj2) < 0)
Syste m . o u t . pr i n t l n (“ o b j 1 i s l e s s than
o b j 2 ” ) ;
10. POLYMORPHISM VIA INTERFACES
10
◉ Define a polymorphism reference through
interface
◼ declare a reference variable of an interface type
Doable o b j ;
◼ the obj reference can be used to point to any object
of any class that implements the Doable interface
◼ the version of doThis depends on the type of
object that obj is referring to:
o b j . d o T h i s ( ) ;
12. MORE EXAMPLES
p u b l ic i n t e r f a c e Speaker
{
p u b l ic void sp e a k( ) ;
}
c l a s s Philosopher extends Human
implements Speaker
{
//
p u b l ic void speak()
{…}
p u b l ic void p o n t i f i c a t e ( )
{…}
}
c l a s s Dog extends Animal
implements Speaker
{
//
p u b l ic void speak()
{
…
}
Speaker g u e s t ;
guest = new Ph i l o sop he r( );
g u e s t . s p e a k ( ) ;
guest = Dog();
g u e s t . s p e a k ( ) ;
11
Speaker s p e c i a l ;
s p e c i a l = new Ph i l o so phe r( ) ;
s p e c i a l . p o n t i f i c a t e ( ) ; /
/compilererror
Speaker s p e c i a l ;
s p e c i a l = new Ph i l o so phe r( ) ;
( ( P h i l o s o p h e r ) s p e c i a l ) . p o n t i f i c a t e ( ) ;
13. INTERFACE HIERARCHIES
◉ Inheritance can be applied to interfaces as well
as classes
◉ One interface can be used as the parent of
another
◉ The child interface inherits all abstract methods
of the parent
◉ A class implementing the child interface must
define all methods from both the parent and
child interfaces
◉ Note that class hierarchies and interface
hierarchies are distinct (they do not overlap)
12