Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
SELF-TEST QUESTION 15.4 ● Inheritance 15.4 Inheritance 207 15.4 Write a class Stack that implements a first-in, last-out structure. Use an array list to hold items of type String. Provide public operations pop and push. push adds an item to the top of the stack. push removes an item from the top of the stack. The class could be used like this: Stack stack = new Stack(); stack.push("Mary"); Real-world systems depend on our ability to classify and categorize. Elephants, tigers, polar bears, horses and cows are all mammals; lead, silver and platinum are metals; savings, current and term deposits are types of bank accounts, and so on. Through classification, we are able to associate characteristics common to all members of a class. All mammals are vertebrates (have backbones), are warm-blooded and have hair on their bodies; all metals have atomic weights; and all bank accounts have balances. We often think of objects as specializations of other objects. Precious metals are specializations of metals, sports cars are specializations of cars, romance novels are specializations of books, and so on. All precious metals are metals but not all metals are precious metals. Similarly, all sports cars are cars and all romance novels are books, but the reverse is not true. Similarly, quadrilaterals and triangles are polygons, and squares and rectangles are special kinds of quadrilaterals. Furthermore, a square is a special kind of rectangle. Extending this notion, we can view one class of objects as a subclass of another. We can also talk about one class being a superclass of another. What does it mean to say that one class is a subclass of another? Intuitively, we mean that the subclass has all the characteristics of the more general class but extends it in some way. Precious metals have all the characteristics of metals but, in addition, they can be distinguished from some metals on the basis of monetary value. Similarly, quadrilaterals are specializations of polygons with four sides. Polygons can have any number of sides. Squares are specializations of quadrilaterals where all four sides have equal length, and adjacent sides are perpendicular to one another. Applying these arguments in reverse, we can describe the superclass of a class as being a generalization of the class. One of the best ways to describe something new to someone else is to describe it in terms of something that is similar, that is, by describing how it differs from something known. An example is that a zebra is a horse with stripes! This concise definition conveys a substantial amount of information to someone familiar with horses but not with zebras. We now extend the cyberspace invaders program so that we can create and display the other objects – the bomb, the laser and the user. We already have a class Alien that describes aliens. We now consider writing a class Bomb to describe bombs. But we soon realize that aliens and bombs have things in common, for example, their x, y coordinates and their size, so although we could write completely separate classes, we can
208 Chapter 15 ■ Object-oriented programming > > exploit the common features of the classes. We do this by writing a class Sprite that embodies the commonality. This name is chosen because, in computer games programs, a sprite is the term for a graphical object. Here it is: class Sprite { } protected int x, y; protected size; public void moveLeft(int amount) { x = x - amount; } public void moveRight(int amount) { x = x + amount; } You can see that the variables and methods within this class are relevant to all the game objects. You will also notice that the variables declared at the head of the class that were described as public, are now described as protected. This means that they are accessible from any subclasses, as we shall see in a moment. We can now write class Alien so as to exploit the class Sprite as follows: class Alien extends Sprite { } private ImageIcon alienImage; public Alien(int newX, int newY, int newSize) { x = newX; y = newY; size = newSize; alienImage = new ImageIcon("c:/alien.jpg"); } public void display(JPanel panel) { Graphics paper = panel.getGraphics(); alienImage.paintIcon(panel, paper, x, y); } and you can see that this is now shorter than it was. The operative word in this code is extends. This is the Java keyword stating that class Alien inherits the features of class Sprite. All the public variables and methods become part of class Alien. The terminology is that Alien is a subclass of Sprite, Sprite is the superclass of Alien, Alien extends Sprite, Sprite is the base class of Alien. > >
- Page 180 and 181: 12.8 Model, view controller (observ
- Page 182 and 183: Figure 12.4 Pipe and Filter pattern
- 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: 206 Chapter 15 ■ Object-oriented
- Page 233 and 234: 210 Chapter 15 ■ Object-oriented
- Page 235 and 236: 212 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
SELF-TEST QUESTION<br />
15.4 ● Inheritance<br />
15.4 Inheritance 207<br />
15.4 Write a class Stack that implements a first-in, last-out structure. Use an<br />
array list to hold items of type String. Provide public operations pop<br />
and push. push adds an item to the top of the stack. push removes an<br />
item from the top of the stack. The class could be used like this:<br />
Stack stack = new Stack();<br />
stack.push("Mary");<br />
Real-world systems depend on our ability to classify and categorize. Elephants, tigers,<br />
polar bears, horses and cows are all mammals; lead, silver and platinum are metals; savings,<br />
current and term deposits are types of bank accounts, and so on. Through classification,<br />
we are able to associate characteristics common to all members of a class. All<br />
mammals are vertebrates (have backbones), are warm-blooded and have hair on their<br />
bodies; all metals have atomic weights; and all bank accounts have balances.<br />
We often think of objects as specializations of other objects. Precious metals are<br />
specializations of metals, sports cars are specializations of cars, romance novels are specializations<br />
of books, and so on. All precious metals are metals but not all metals are<br />
precious metals. Similarly, all sports cars are cars and all romance novels are books, but<br />
the reverse is not true. Similarly, quadrilaterals and triangles are polygons, and squares<br />
and rectangles are special kinds of quadrilaterals. Furthermore, a square is a special<br />
kind of rectangle. Extending this notion, we can view one class of objects as a subclass<br />
of another. We can also talk about one class being a superclass of another.<br />
What does it mean to say that one class is a subclass of another? Intuitively, we mean<br />
that the subclass has all the characteristics of the more general class but extends it in<br />
some way. Precious metals have all the characteristics of metals but, in addition, they can<br />
be distinguished from some metals on the basis of monetary value. Similarly, quadrilaterals<br />
are specializations of polygons with four sides. Polygons can have any number of<br />
sides. Squares are specializations of quadrilaterals where all four sides have equal length,<br />
and adjacent sides are perpendicular to one another. Applying these arguments in<br />
reverse, we can describe the superclass of a class as being a generalization of the class.<br />
One of the best ways to describe something new to someone else is to describe it in<br />
terms of something that is similar, that is, by describing how it differs from something<br />
known. An example is that a zebra is a horse with stripes! This concise definition conveys<br />
a substantial amount of in<strong>for</strong>mation to someone familiar with horses but not with zebras.<br />
We now extend the cyberspace invaders program so that we can create and display<br />
the other objects – the bomb, the laser and the user. We already have a class Alien that<br />
describes aliens. We now consider writing a class Bomb to describe bombs. But we soon<br />
realize that aliens and bombs have things in common, <strong>for</strong> example, their x, y coordinates<br />
and their size, so although we could write completely separate classes, we can