






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







08 1
Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7
08 2
An Abstract Data Type (ADT) is a contract that specifies the behavior of methods that operate on a data structure without exposing the representation of the data structure. In general, there are many different implementations of a given ADT. In this lecture:
08 3 Immutable Point Contract // Constructor Method public IPoint (int x, int y); /* Create an immutable point with coordinates x and y */ // Instance Methods public int getX(); // Return the x-coordinate of this point. public int getY(); // Return the y-coordinate of this point public String toString(); // Return string of form (x,y) 08 4 Two Implementations of Immutable Points
08 7 An IPoint Abstract Class public abstract class IPoint { public abstract int getX(); public abstract int getY(); public String toString() { return "(" + getX() + "," + getY() + ")"; } } public class IPointVars extends IPoint { โฆ same impl. as before except no toString() method โฆ} public class IPointArray extends IPoint { โฆ same impl. as before except no toString () method โฆ} public static int sumCoords (IPoint p) { return p.getX() + p.getY(); } sumCoords(new IPointVars(1,2)) โ 3 sumCoords(new IPointArray(3,4)) โ 7
An abstract class may have concrete methods; these can use abstract methods. 08 8 Interfaces vs. Abstract Classes Interfaces are used solely to specify a contract (API/ADT)
08 9 Interfaces and Abstract Classes in Java Some standard Java Interfaces:
08 13 Iterator Removal Example
08 14 Vector Filtering Gotcha!
08 15 Stack Interface import java.util.; // imports Iterator and Iterable /* Interface to mutable stacks with elements of type T. A stack is a last-in-first-out (LIFO) collection with duplicates. WARNING: This CS230 Stack is an interface, and is different from the class named java.util.Stack. */ public interface Stack
08 19