Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

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. > >

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!