04-InterfacesAndTypes - 6/15/2011 Interfaces CS/ENGRD 2110...

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

View Full Document Right Arrow Icon
6/15/2011 1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2011 Thorsten Joachims Lecture 4: Interfaces and Types 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 2 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 3 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) {. ..} } 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 Ipod, Ipad {. ..} 4 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 Examples 5 Map<String, Command> h = new HashMap<String, Command>(); List<Object> t = new ArrayList<Object>(); Set<Integer> s = new HashSet<Integer>(); 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? 6
Background image of page 1

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

View Full DocumentRight Arrow Icon
6/15/2011 2 7 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 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 09/05/2011 for the course CS 211 taught by Professor Staff during the Spring '08 term at Cornell University (Engineering School).

Page1 / 6

04-InterfacesAndTypes - 6/15/2011 Interfaces CS/ENGRD 2110...

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