09-stack_queue - CSE 143 Lecture 9 Interfaces; Stacks and...

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

View Full Document Right Arrow Icon
1 CSE 143 Lecture 9 Interfaces; Stacks and Queues slides created by Marty Stepp http://www.cs.washington.edu/143/ 2 Related classes Consider the task of writing classes to represent shapes such as Circle , Rectangle , and Triangle . Certain operations are common to all shapes: – perimeter – area
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 3 Shape area and perimeter • Circle: area = π r 2 perimeter = 2 π r • Rectangle: area = w h perimeter = 2 w + 2 h • Triangle: area = ( s ( - a ) ( - b ) ( - c )) where = ½ ( + + ) perimeter = + + c r w h a b c 4 Printing shape information • We'd like our client code to be able to print the area and perimeter of our shapes. – Of course, we would use methods. .. public static void printCircleInfo(Circle c) { System.out.println("area = " + c.area()); System.out.println("perimeter = " + c.perimeter()); } public static void printTriangleInfo(Triangle t) { System.out.println("area = " + t.area()); System.out.println("perimeter = " + t.perimeter()); } public static void printRectangleInfo(Rectangle r) { System.out.println("area = " + r.area()); System.out.println("perimeter = " + r.perimeter()); } Redundancy—ewwwwwww!!!! What can we do about this?
Background image of page 2
3 5 Printing shape information • What about having shapes inherit from a superclass Shape that has the area and perimeter methods so we can have polymorphic methods? public class Rectangle extends Shape { . .. } public class Triangle extends Shape { . .. } public class Circle extends Shape { . .. } public static void printShapeInfo(Shape s) { System.out.println("area = " + s.area()); System.out.println("perimeter = " + s.perimeter()); } • What is wrong with this solution? - What does the Shape class look like? 6 Inheritance doesn’t apply public class Shape { public double area() { // what goes here? } public double perimeter() { // what goes here? } } • This doesn’t work—different shapes compute the area and perimeter differently!
Background image of page 3

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

View Full DocumentRight Arrow Icon
7 Interfaces (9.5) interface : A list of methods that a class can promise to implement. – Inheritance gives you an is-a relationship and code sharing. • A Lawyer can be treated as an Employee and inherits its code. – Interfaces give you an is-a relationship without code sharing. • A Rectangle object can be treated as a Shape but inherits no code. – Analogous to non-programming idea of roles or certifications: • "I'm certified as a CPA accountant. This assures you I know how to do taxes, audits, and consulting." • "I'm 'certified' as a Shape, because I implement the Shape interface. This assures you I know how to compute my area and perimeter." 8 Interface syntax public interface name { public type name ( type name , ... , type name ); public type name ( type name , ... , type name ); ... public
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 18

09-stack_queue - CSE 143 Lecture 9 Interfaces; Stacks and...

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

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