{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04-InterfacesAndTypes

04-InterfacesAndTypes - Interfaces CS/ENGRD 2110...

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

View Full Document Right Arrow Icon
2/3/2011 1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2010 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
Image of page 1

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

View Full Document Right Arrow Icon
2/3/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!
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern