Lecture 25: Java Interfaces
Download
Report
Transcript Lecture 25: Java Interfaces
Java Interfaces
the public interface of a class
the interface reference type
examples
(Note: not graphical user interface)
CS2200
Java Interfaces
1
How should we design classes?
(from a previous lecture)
1. obtain a statement of the problem
2. sketch a sample scenario
3. work out what objects are involved
(do the following one class at a time:)
4. work out how those objects are meant to behave
5. design the interface for the class
6. define the variables
7. implement the methods
the interface only shows the
8. test the class
public methods and data
● it does not show private
data or methods
● it does not state how the
class is implemented
CS2200
Java Interfaces
2
Determining 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
CS2200
Java Interfaces
3
Data 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
}
CS2200
Java Interfaces
4
Java 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)
states that
this is an
interface,
not a class
CS2200
public interface MyStack {
public int size();
public boolean isEmpty();
public Object top();
public void push(Object elt);
public Object pop();
}
Java Interfaces
note no
bodies for
the methods
5
interfaces 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
CS2200
Java Interfaces
6
using 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
CS2200
Java Interfaces
7
Example
import java.util.ArrayList;
public class ALStack<E> implements MyStack {
private ArrayList<E> al;
public ALStack() {
promises that we
al = new ArrayList<E>(); will provide bodies for
}
all the MyStack methods
public int size() { return al.size(); }
//and versions of isEmpty, push, pop and top, as
//in the previous lecture
}
CS2200
Java Interfaces
8
Example (cont)
public class ArrayStack implements MyStack {
private int capacity;
private Object[] s;
private int top = -1;
public ArrayStack(int reqdCapacity) {
capacity = reqdCapacity;
s = new Object[capacity];
}
public int size() { return top+1; }
//and versions of isEmpty, push, pop and top ...
}
CS2200
Java Interfaces
9
More 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 ALStack<Book>();
● Dynamic method binding: decide at run-time which method
to run, based on the referenced object
s.push(new Book());
CS2200
Java Interfaces
10
What'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
CS2200
Java Interfaces
11
Example: 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());
}
CS2200
Java Interfaces
12
Example: 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());
}
only 1 thing
to change
public void method3(MyStack st) {
st.push(new Pig());
}
CS2200
Java Interfaces
13
Summary
● 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
CS2200
Java Interfaces
14
Next lecture ...
… Java Collections Framework
CS2200
Java Interfaces
15