Title: Java Interfaces
1Java Interfaces
- the public interface of a class
- the interface reference type
- examples
- (Note not graphical user interface)
2How should we design classes?(from a previous
lecture)
- obtain a statement of the problem
- sketch a sample scenario
- work out what objects are involved
- (do the following one class at a time)
- work out how those objects are meant to behave
- design the interface for the class
- define the variables
- implement the methods
- test the class
- the interface only shows the public methods and
data - it does not show private data or methods
- it does not state how the class is implemented
3Determining the interface
- before writing a class definition, determine the
interface - the set of services we offer to clients
- similarly, if defining data structures, we should
first determine the interface - stacks support a constructor, push, pop, size,
isEmpty, and top - queues offer a constructor, enqueue, dequeue,
size, isEmpty and front - data structures people refer to the interface as
the - abstract data type
4Data Abstraction
- if the interface remains the same, clients don't
need to be changed, even if the implementation
behind the interface changes
public class Time private int timeInSecs
//public methods
public class Time private int hours
private int minutes private int secs
//same public methods //but with different
//bodies
5Java Interfaces
- Java allows us to take this one stage further, by
formally recording the interface as a Java
interface - a java interface is just a collection of abstract
methods (i.e. we state the signatures, but not
the bodies)
public interface MyStack public int size()
public boolean isEmpty() public Object
top() public void push(Object elt) public
Object pop()
6interfaces vs classes
- a class definition can contain instance/class
variables and instance/class methods, including
both the signature and the body - a java interface contains only the signatures of
public instance methods (and named constants) - a java interface acts like a specification
- it says what it means to be e.g. a stack
- to be a stack, an object must offer at least
those methods in its public interface
7using Java interfaces
- Java allows us to tell the compiler that a class
will implement an interface - regard it as a contract stating that you will
meet the specification - any class that implements an interface must
provide implementations of the public methods (or
it must be abstract, and its subclasses provide
them) - the compiler will check, and if the bodies are
not provided, it won't compile
8Example
- import java.util.ArrayList
- public class ALStackltEgt implements MyStack
- private ArrayListltEgt al
- public ALStack()
- al new ArrayListltEgt()
-
- public int size() return al.size()
- //and versions of isEmpty, push, pop and top,
as - //in the previous lecture
9Example (cont)
public class ArrayStack implements MyStack
private int capacity private Object s
private int top -1 public ArrayStack(int
reqdCapacity) capacity reqdCapacity
s new Objectcapacity public int
size() return top1 //and versions of
isEmpty, push, pop and top ...
10More Polymorphism
- Polymorphism
- a variable of a superclass type may refer to
objects of that class or of its subclasses - a variable of Java interface type may refer to
objects of any class that implements the
interface - MyStack s
- s new ArrayStack(20)
- s new ALStackltBookgt()
- Dynamic method binding decide at run-time which
method to run, based on the referenced object - s.push(new Book())
11What's the point?
- using Java interfaces polymorphically gives you
client code that is much easier to modify - how much effort would be involved to change from
an ArrayStack to an ALStack if we hadn't used an
interface? - In program design and development, you will
probably frequently change the data structures a
program uses, so interfaces gives a significant
improvement in maintainability
12Example client not using interface
- public void method1()
- ArrayStack s new ArrayStack(10)
- s.push(new Cow())
- method2(s)
- method3(s)
-
- public void method2(ArrayStack st)
- System.out.println(st.top())
-
- public void method3(ArrayStack st)
- st.push(new Pig())
13Example client using interface
- public void method1()
- MyStack s new ArrayStack(10)
- s.push(new Cow())
- method2(s)
- method3(s)
-
- public void method2(MyStack st)
- System.out.println(st.top())
-
- public void method3(MyStack st)
- st.push(new Pig())
14Summary
- An interface is like an abstract class - it
specifies what its methods should look like, but
gives no body - if a class implements an interface, it is
equivalent to signing a contract saying that it
will provide a body for the specified method -
Java will not compile the program unless we
provide the method definition - we can refer to an object as though it were an
object of the interface, and invoke the interface
methods
15Next lecture ...
- Java Collections Framework