Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
15.5 Polymorphism 211 which is clumsy and long-winded. This uses the keyword instanceof to ask if an object is a member of a named class. If there are a large number of graphical objects, there are a correspondingly large number of if statements. Avoiding this complexity demonstrates how powerful and concise polymorphism is. As we have seen in this small example, polymorphism often makes a segment of program smaller and neater through the elimination of a series of if statements. But this achievement is much more significant than it may seem. It means that such statements as: sprite.display(paper); know nothing about the possible variety of objects that may be used as the value of sprite. So information hiding (already present in large measure in an OOP) is extended. We can check this by assessing how much we would need to change this program to accommodate some new type of graphical object (some additional subclass of Sprite), say a laser. The answer is that we would not need to modify it at all – we could simply add the new object. This means that the program is enormously flexible. Thus polymorphism enhances modularity, reusability and maintainability. Polymorphism helps construct programs that are: ■ concise (shorter than they might otherwise be) ■ modular (unrelated parts are kept separate) ■ easy to change and adapt (for example, introducing new objects). In general, the approach to exploiting polymorphism within a particular program is as follows: 1. identify any similarities (common methods and variables) between any objects or classes in the program 2. design a superclass that embodies the common features of the classes 3. design the subclasses that describe the distinctive features of each of the classes, whilst inheriting the common features from the superclass 4. identify any place in the program where the same operation must be applied to any of the similar objects. It may be tempting to use if statements at this location. Instead, this is the place to use polymorphism. 5. make sure that the superclass contains an abstract method corresponding to the method that is to be used polymorphically. The code fragment shown above, with an array list and a for loop, is an example of a commonly occurring situation in software, where the entire contents of a collection are processed. It is so common that some languages provide a foreach control structure. In Java, the above for loop can be rewritten more concisely as: for (Object item : game) { ((Sprite) item).display(paper); } >
212 Chapter 15 ■ Object-oriented programming > Each time that the for statement repeats, it obtains the next element from the array list game. 15.6 ● Single versus multiple inheritance As we have seen, Java supports single inheritance – a class can inherit from only one immediate superclass. Seen as a class diagram, the relationships between classes appear as a tree (a computer science tree, with the root at the top). Smalltalk, Ada, C# and Visual Basic.Net also provide single inheritance. However, the widely used language C++ provides multiple inheritance, as does Eiffel. In such a language, a class can inherit from not just one but several superclasses. In life we are not just a person, we also belong to other categories, such as brothers, daughters, soccer lovers, carnivores. So a class representing a person is a subclass of all these superclasses, inheriting variables and methods from them all. There is no doubt that multiple inheritance is more complicated – both to provide in the language and to use. C++ was widely seen as an overcomplicated language and subsequent languages, such as Java and C#, have seen simplifications in many areas, including abandoning multiple inheritance in favor of single. In some languages, including Java and C#, one role of multiple inheritance has been replaced by the interface facility described in Chapter 16 on programming in the large. 15.7 ● Generics The strong typing philosophy of programming languages like Java and Ada can have a detrimental effect on programming efficiency. For example, suppose we defined a stack of strings class with the normal stack operations of push and pop, as posed in the self-test question above. If we subsequently needed another stack type but one in which the elements were Booleans rather than strings then clearly the specification and implementation would be identical apart from the different stack element types. In some languages, our only recourse would be to duplicate the stack code, but with minor differences. A more powerful stack abstraction is required which allows the stack element type to be parameterized. We will use the Java cyberspace invaders game discussed above to see how generics can be used. An array list named game contains objects representing various items (alien, bomb, laser) at various positions within a panel. To display all the shapes, we execute a loop: for (int s = 0, s < game.size(); s++) { sprite sprite = (Sprite) game.get(s); sprite.display(paper); } Notice that the objects retrieved from the array list need to be casted into Sprite objects using a casting operator, (Sprite) in this case. This is because an array list >
- Page 184 and 185: Figure 12.6 Layers in a distributed
- Page 186 and 187: Answers to self-test questions 163
- Page 188 and 189: CHAPTER 13 Refactoring This chapter
- Page 190 and 191: 13.3 ● Move Method 13.6 Inline Cl
- Page 192 and 193: class Sprite Instance variables x y
- Page 194 and 195: Summary Summary 171 it is making po
- Page 196: PART C PROGRAMMING LANGUAGES
- Page 199 and 200: 176 Chapter 14 ■ The basics and a
- Page 201 and 202: 178 Chapter 14 ■ The basics > > >
- Page 203 and 204: 180 Chapter 14 ■ The basics > Ear
- Page 205 and 206: 182 Chapter 14 ■ The basics > Cas
- Page 207 and 208: 184 Chapter 14 ■ The basics > > >
- Page 209 and 210: 186 Chapter 14 ■ The basics > } }
- Page 211 and 212: 188 Chapter 14 ■ The basics Unfor
- Page 213 and 214: 190 Chapter 14 ■ The basics Ada d
- Page 215 and 216: 192 Chapter 14 ■ The basics The w
- Page 217 and 218: 194 Chapter 14 ■ The basics In a
- Page 219 and 220: 196 Chapter 14 ■ The basics > str
- Page 221 and 222: 198 Chapter 14 ■ The basics Answe
- Page 223 and 224: CHAPTER 15 Object-oriented programm
- Page 225 and 226: 202 Chapter 15 ■ Object-oriented
- Page 227 and 228: 204 Chapter 15 ■ Object-oriented
- Page 229 and 230: 206 Chapter 15 ■ Object-oriented
- Page 231 and 232: 208 Chapter 15 ■ Object-oriented
- Page 233: 210 Chapter 15 ■ Object-oriented
- Page 237 and 238: 214 Chapter 15 ■ Object-oriented
- Page 239 and 240: 216 Chapter 15 ■ Object-oriented
- Page 241 and 242: 218 Chapter 15 ■ Object-oriented
- Page 243 and 244: 220 Chapter 15 ■ Object-oriented
- Page 245 and 246: 222 Chapter 16 ■ Programming in t
- Page 247 and 248: 224 Chapter 16 ■ Programming in t
- Page 249 and 250: 226 Chapter 16 ■ Programming in t
- Page 251 and 252: 228 Chapter 16 ■ Programming in t
- Page 253 and 254: 230 Chapter 16 ■ Programming in t
- Page 255 and 256: 232 Chapter 16 ■ Programming in t
- Page 257 and 258: 234 Chapter 16 ■ Programming in t
- Page 259 and 260: 236 Chapter 16 ■ Programming in t
- Page 261 and 262: 238 Chapter 17 ■ Software robustn
- Page 263 and 264: 240 Chapter 17 ■ Software robustn
- Page 265 and 266: 242 Chapter 17 ■ Software robustn
- Page 267 and 268: 244 Chapter 17 ■ Software robustn
- Page 269 and 270: 246 Chapter 17 ■ Software robustn
- Page 271 and 272: 248 Chapter 17 ■ Software robustn
- Page 273 and 274: 250 Chapter 17 ■ Software robustn
- Page 275 and 276: 252 Chapter 17 ■ Software robustn
- Page 277 and 278: 254 Chapter 17 ■ Software robustn
- Page 279 and 280: 256 Chapter 17 ■ Software robustn
- Page 281 and 282: 258 Chapter 17 ■ Software robustn
- Page 283 and 284: 260 Chapter 18 ■ Scripting GNU/Li
212 Chapter 15 ■ Object-oriented programming<br />
><br />
Each time that the <strong>for</strong> statement repeats, it obtains the next element from the array<br />
list game.<br />
15.6 ● Single versus multiple inheritance<br />
As we have seen, Java supports single inheritance – a class can inherit from only one<br />
immediate superclass. Seen as a class diagram, the relationships between classes appear<br />
as a tree (a computer science tree, with the root at the top). Smalltalk, Ada, C# and<br />
Visual Basic.Net also provide single inheritance.<br />
However, the widely used language C++ provides multiple inheritance, as does<br />
Eiffel. In such a language, a class can inherit from not just one but several superclasses.<br />
In life we are not just a person, we also belong to other categories, such as brothers,<br />
daughters, soccer lovers, carnivores. So a class representing a person is a subclass of all<br />
these superclasses, inheriting variables and methods from them all.<br />
There is no doubt that multiple inheritance is more complicated – both to provide<br />
in the language and to use. C++ was widely seen as an overcomplicated language and<br />
subsequent languages, such as Java and C#, have seen simplifications in many areas,<br />
including abandoning multiple inheritance in favor of single. In some languages,<br />
including Java and C#, one role of multiple inheritance has been replaced by the interface<br />
facility described in Chapter 16 on programming in the large.<br />
15.7 ● Generics<br />
The strong typing philosophy of programming languages like Java and Ada can have a<br />
detrimental effect on programming efficiency. For example, suppose we defined a stack of<br />
strings class with the normal stack operations of push and pop, as posed in the self-test<br />
question above. If we subsequently needed another stack type but one in which the elements<br />
were Booleans rather than strings then clearly the specification and implementation<br />
would be identical apart from the different stack element types. In some languages, our only<br />
recourse would be to duplicate the stack code, but with minor differences. A more powerful<br />
stack abstraction is required which allows the stack element type to be parameterized.<br />
We will use the Java cyberspace invaders game discussed above to see how generics<br />
can be used. An array list named game contains objects representing various items<br />
(alien, bomb, laser) at various positions within a panel. To display all the shapes, we execute<br />
a loop:<br />
<strong>for</strong> (int s = 0, s < game.size(); s++) {<br />
sprite sprite = (Sprite) game.get(s);<br />
sprite.display(paper);<br />
}<br />
Notice that the objects retrieved from the array list need to be casted into Sprite<br />
objects using a casting operator, (Sprite) in this case. This is because an array list<br />
>