L04cs2110fa08 - Interfaces & Types Lecture 4 CS2110...

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

View Full Document Right Arrow Icon
Lecture 4 CS2110 – Fall 2008
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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) {. ..} }
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 {. ..}
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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 6
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?
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

L04cs2110fa08 - Interfaces &amp; Types Lecture 4 CS2110...

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

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