L04cs2110fa08-6up - Interfaces Interfaces & Types Lecture 4...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 4 CS2110 – Fall 2008 2 Interfaces What is an interface ? Informally, it is a specification of how an agent interacts with the outside world Java has a construct called interface which is used formally for this purpose – an interface describes how a class interacts with its clients – method names, argument/return types, fields 3 Java interface • name of interface: IPuzzle • a class implements this interface by implementing public instance methods as specified in the interface • the class may implement other methods interface IPuzzle { void scramble(); int tile(int r, int c); boolean move(char d); } class IntPuzzle implements IPuzzle { public void scramble() {. ..} public int tile(int r, int c) {. ..} public boolean move(char d) {. ..} } 4 Notes An interface is not a class! – cannot be instantiated – incomplete specification class header must assert implements I for Java to recognize that the class implements interface I A class may implement several interfaces: class X implements IPuzzle, IPod {. ..} 5 Why an interface construct? • good software engineering – specify and enforce boundaries between different parts of a team project • can use interface as a type – allows more generic code – reduces code duplication 6 • Lots of examples in Java Why an interface construct? Map<String, Command> h = new HashMap<String, Command>(); List<Object> t = new ArrayList<Object>(); Set<Integer> s = new HashSet<Integer>();
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
7 Example of code duplication Suppose we have two implementations of puzzles: – class IntPuzzle uses an int to hold state – class ArrayPuzzle uses an array to hold state Say the client wants to use both implementations – perhaps for benchmarking both implementations to pick the best one – client code has a display method to print out puzzles What would the display method look like? 8 class Client{ IntPuzzle p1 = new IntPuzzle(); ArrayPuzzle p2 = new ArrayPuzzle(); ...display(p1). ..display(p2). .. public static void display( IntPuzzle p){ for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) System.out.println(p.tile(r,c)); } public static void display( ArrayPuzzle p){ for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) System.out.println(p.tile(r,c)); } } Code duplicated because IntPuzzle and ArrayPuzzle are different 9 Observation Two display methods are needed because IntPuzzle and ArrayPuzzle are different types, and parameter p must be one or the other but the code inside the two methods is identical! – code relies only on the assumption that the object
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2010 for the course CS 2110 at Cornell University (Engineering School).

Page1 / 6

L04cs2110fa08-6up - Interfaces Interfaces & Types Lecture 4...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online