{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

12a-interfaces - CSE 143 Lecture 12(A Interfaces reading...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 12 (A) Interfaces reading: 9.5, 11.1 slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

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

View Full Document Right Arrow Icon
2 Related classes Consider the task of writing classes to represent 2D shapes such as Circle , Rectangle , and Triangle . Certain operations are common to all shapes: perimeter: distance around the outside of the shape area: amount of 2D space occupied by the shape Every shape has these, but each computes them differently.
Background image of page 2
3 Shape area and perimeter • Circle (as defined by radius r ): area = π r 2 perimeter = 2 π r • Rectangle (as defined by width w and height h ): area = w h perimeter = 2 w + 2 h • Triangle (as defined by side lengths a , b , and c ) area = ( s ( s - a ) ( s - b ) ( s - c )) where s = ½ ( a + b + c ) perimeter = a + b + c r w h a b c
Background image of page 3

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

View Full Document Right Arrow Icon
4 Common behavior • Suppose we have 3 classes Circle , Rectangle , Triangle . Each has the methods perimeter and area . • We'd like our client code to be able to treat different kinds of shapes in the same way: Write a method that prints any shape's area and perimeter. Create an array to hold a mixture of the various shape objects. Write a method that could return a rectangle, a circle, a triangle, or any other kind of shape. Make a DrawingPanel display many shapes on screen.
Background image of page 4
5 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.
Background image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}