Java Interfaces the public interface of a class
Java Interfaces the public interface of a class the interface reference type examples (Note: not graphical user interface) CS 2200 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 CS 2200 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, is. Empty, and top ● queues offer a constructor, enqueue, dequeue, size, is. Empty and front ● data structures people refer to the interface as the abstract data type CS 2200 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 time. In. Secs; //public methods public class Time { private int hours; private int minutes private int secs; } //same public methods //but with different //bodies } CS 2200 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 CS 2200 public interface My. Stack { public int size(); public boolean is. Empty(); 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 CS 2200 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 CS 2200 Java Interfaces 7
Example import java. util. Array. List; public class ALStack<E> implements My. Stack { private Array. List<E> al; public ALStack() { al = new Array. List<E>(); } promises that we will provide bodies for all the My. Stack methods public int size() { return al. size(); } //and versions of is. Empty, push, pop and top, as //in the previous lecture } CS 2200 Java Interfaces 8
Example (cont) public class Array. Stack implements My. Stack { private int capacity; private Object[] s; private int top = -1; public Array. Stack(int reqd. Capacity) { capacity = reqd. Capacity; s = new Object[capacity]; } public int size() { return top+1; } //and versions of is. Empty, push, pop and top. . . } CS 2200 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 My. Stack s; s = new Array. Stack(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()); CS 2200 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 Array. Stack 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 CS 2200 Java Interfaces 11
Example: client not using interface public void method 1() { Array. Stack s = new Array. Stack(10); s. push(new Cow()); method 2(s); method 3(s); } public void method 2(Array. Stack st) { System. out. println(st. top()); } public void method 3(Array. Stack st) { st. push(new Pig()); } CS 2200 Java Interfaces 12
Example: client using interface public void method 1() { My. Stack s = new Array. Stack(10); s. push(new Cow()); method 2(s); method 3(s); } public void method 2(My. Stack st) { System. out. println(st. top()); } only 1 thing to change public void method 3(My. Stack st) { st. push(new Pig()); } CS 2200 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 CS 2200 Java Interfaces 14
Next lecture. . . … Java Collections Framework CS 2200 Java Interfaces 15
- Slides: 15