DOC PREVIEW
UB CSE 115 - Chapter 6 Polymorphism

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© 2006-2007 Carl Alphonce & Adrienne Decker 1 ; Chapter 6 Polymorphism ; Pedagogic Goals Introduce the notion that polymorphism allows us to model inherent differences in the implementation of identical capabilities. Introduce the notion that polymorphism is a form of selection, based on the type of an object. Present design patterns as structuring mechanisms for code which allow us to build software systems possessing certain desirable characteristics. Introduction In the real world we often come across systems with the same capabilities which nonetheless differ quite a bit in the realization of those capabilities. Example 1 Four-legged mammals have two different forms of walk. Some four-legged mammals, like dogs, walk by moving their left-rear and right-front legs together, and right-rear and left-front legs together. Others, like giraffes, walk by moving both legs on one side together. Both dogs and giraffes have the capability to walk, but they do so in quite different ways. http://www.earthlife.net/mammals/locomotion.html Because dogs and giraffes both walk but they walk differently, we say that dogs and giraffes are polymorphic in their walking capability. In this chapter we will explore many different systems which exhibit polymorphic behavior naturally. We will also learn how to build object oriented models which exploit the natural polymorphism of systems to yield flexible and easily extensible software. Polymorphism is natural Polymorphism refers the ability of different types of things to react differently to the same stimuli. In our object oriented systems, this means that different types of objects can respond differently to the same method call. Polymorphism is a very natural, everyday phenomenon. We unconsciously make use of polymorphism several times a day. Before we look more closely at what polymorphism looks like in an executable model, let us consider several more examples of polymorphic behavior.Error! Use the Home tab to apply Chapter Title to the text that you want to appear here. Polymorphism 2 9/29/2008 Example 2 One of the authors is a bit of an old-timer. He has a cassette deck hooked up to his stereo. He also has a CD player hooked up. The other author, who is far more hip, has an MP3 player. All of these devices can play music. They all have a “play” functionality. However, how they play their music is quite different. Playing is polymorphic! The tape deck uses a magnetic head, called the read head, to read information from a magnetic tape which is passing below the head. The tape is pulled along from one reel of the cassette tape to another reel in the cassette by a little motor. The CD player uses an optical read head to read digitally encoded information from the pitted surface of a CD as a stream of bits. The CD is rotated about its center by a motor. When the surface of the CD passes under the read head, laser light is reflected off the surface of the CD – depending on how the light reflects off the surface the CD player is able to distinguish 0’s from 1’s and thereby read each bit of information from the CD. The MP3 player has no moving parts. It reads similar kinds of digital information from its memory as the CD player extracts from the surface of a CD. Even though tape decks, CD players and MP3 players interpret a “play” instruction very differently, they can all play the same music. This is polymorphism at work. Example 3 Once upon a time printing terminals with dot matrix print heads were the latest thing. Nowadays inkjet printers and laser printers are common. A document can be printed on any of these kinds of printers, but the means of forming an image on a piece of paper is quite different. The Digital Equipment Corporation DEC-Writer was a printing terminal with a single dot-matrix print head. A dot-matrix print head has several pins which can be pushed out against an inked ribbon, which is pressed against a piece of paper, thereby making an impression on the paper. Characters were made by different patterns of pin impressions, usually on a 5 by 7 grid. The inkjet printer and the laser printer use two very different printing technologies. Printing with an inkjet printer works by spraying a small amount of ink from a nozzle onto the page. Printing with a laser printer works by charging points on a rotating drum; charged regions pick up toner as the drum rotates, which is then melted onto the paper by a high-temperature fuser. In all cases the printer “prints” a page of information, but in a decidedly different way. The software which is sending information to the printer need not know what printer the information is being sent to – the printer knows how to print in its own polymorphic way.Polymorphism is natural © 2006-2007 Carl Alphonce & Adrienne Decker 3 When we model systems in object oriented terms we want to be able to capture these aspects of systems. These are examples of polymorphic behaviors. In object oriented terms we model polymorphic behaviors by defining methods polymorphically. One way a method can be defined polymorphically occurs when different classes, all of which implement the same interface, provide distinct definitions for a method specified in the interface. Example 4 There are two basic kinds of automatic car washes: mechanical (apparently called “cloth-friction”) and touchless. A mechanical car wash uses brushes of some sort to scrub the outside of the car, while a touchless car wash uses powerful water sprays and strong chemicals to clean the car. Many of these systems are under computer control. As far as the client of the carwash is concerned the possible operations that the carwash can perform are the same, such as rinse, wash, wax, undercarriage spray and dry. The way the computer controls need to implement these operations is quite different. This is yet another example of polymorphic behavior.1 The basic framework for implementing the car wash control could be something like the following: 1 For more information about car washes, see en.wikipedia.org/wiki/Carwash auto.howstuffworks.com/car-wash.htmError! Use the Home tab to apply Chapter Title to the text that you want to appear here. Polymorphism 4 9/29/2008 Let us suppose that in a typical car wash installation there is a user interface which lets a person select the type of wash that they want for their car. The user interface may have buttons like the ones


View Full Document

UB CSE 115 - Chapter 6 Polymorphism

Download Chapter 6 Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 6 Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 6 Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?