160 likes | 194 Views
Java Interfaces. the public interface of a class the interface reference type examples (Note: not graphical user interface). How should we design classes? (from a previous lecture). obtain a statement of the problem sketch a sample scenario work out what objects are involved
E N D
Java Interfaces • the public interface of a class • the interface reference type • examples • (Note: not graphical user interface)
How 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
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
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 }
states that this is an interface, not a class note no bodies for the methods 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) public interface MyStack { public int size(); public boolean isEmpty(); public Object top(); public void push(Object elt); public Object pop(); }
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
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
promises that we will provide bodies for all the MyStack methods Example • import java.util.ArrayList; • public class ALStack<E> implements MyStack { • private ArrayList<E> al; • public ALStack() { • al = new ArrayList<E>(); • } • public int size() { return al.size(); } • //and versions of isEmpty, push, pop and top, as • //in the previous lecture • }
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 ... • }
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());
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
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()); • }
only 1 thing to change 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()); • } • public void method3(MyStack st) { • st.push(new Pig()); • }
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
Next lecture ... • … Java Collections Framework