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

15.2 Encapsulation 203 Often (though not in the Alien example shown above) there will need to be additional methods that the class needs in order to carry out its tasks. These are private methods that need not and therefore should not be accessible from outside the class. A class represents a real fusion of data and actions. A class extends the built-in data types provided by the language, so that the programmer can invent data suitable for the problem being solved. The programmer specifies how the data can be manipulated and thus creates truly abstract data. The advantages of encapsulation are: ■ to make useful classes generally available ■ to use a class without the need to know how it works ■ to have the flexibility to modify a class without affecting its users. Properties We have seen that it is very bad practice to make public any of the instance variables of a class. Some languages, for example, C# and Visual Basic, provide a mechanism that simulates accessing variables directly. This feature, called properties, enables users to have convenient but controlled access to the data associated with an object. In general there are two distinct kinds of access to data: 1. reading a value – called get access 2. writing the value – called set access. For example, suppose we want to allow a user of an alien object to refer to (get) the x coordinate of the alien and display its value in a text field. The value is held in the private variable named x at the top of the class. Using a property named xCoord, we can write: textField.setText(Integer.toString(alien.xCoord)); Suppose also that we want the user to be able to change (set) the value of the x coordinate. Using the property facility we can write: alien.xCoord = 56; The way to provide these facilities is to write a property. Here is the revised class that includes the property code: public class AlienWithProperties { private int x; private int y; private int size; private ImageIcon alienImage; >

204 Chapter 15 ■ Object-oriented programming > > } public Alien(int newX, int newY, int newSize) { x = newX; y = newY; size = newSize; alienImage = new ImageIcon("c:/alien.jpg"); } public void moveLeft(int amount) { x = x - amount; } public void moveRight(int amount) { x = x + amount; } public void display(JPanel panel) { Graphics paper = panel.getGraphics(); alienImage.paintIcon(panel, paper, x, y); } public int xCoord { get { return x; } set { x = value; } } The header for a property looks similar to a method header, except that there are no brackets to specify parameters. The property consists of two complementary components – one has get as the heading and the other has set as the heading. The get part is like a function method – it returns the desired value. The set part is like a method – it assigns the value using the special keyword value as shown. Note that this code is in the style of Java, but it is not Java, since Java does not support a property mechanism. If we only need a way of viewing a property (but not changing its value), we write the property declaration like this: public int xCoord { get { return x; } } >

15.2 Encapsulation 203<br />

Often (though not in the Alien example shown above) there will need to be additional<br />

methods that the class needs in order to carry out its tasks. These are private<br />

methods that need not and there<strong>for</strong>e should not be accessible from outside<br />

the class.<br />

A class represents a real fusion of data and actions. A class extends the built-in data<br />

types provided by the language, so that the programmer can invent data suitable <strong>for</strong> the<br />

problem being solved. The programmer specifies how the data can be manipulated and<br />

thus creates truly abstract data.<br />

The advantages of encapsulation are:<br />

■ to make useful classes generally available<br />

■ to use a class without the need to know how it works<br />

■ to have the flexibility to modify a class without affecting its users.<br />

Properties<br />

We have seen that it is very bad practice to make public any of the instance variables<br />

of a class. Some languages, <strong>for</strong> example, C# and Visual Basic, provide a mechanism that<br />

simulates accessing variables directly. This feature, called properties, enables users to<br />

have convenient but controlled access to the data associated with an object. In general<br />

there are two distinct kinds of access to data:<br />

1. reading a value – called get access<br />

2. writing the value – called set access.<br />

For example, suppose we want to allow a user of an alien object to refer to (get) the<br />

x coordinate of the alien and display its value in a text field. The value is held in the<br />

private variable named x at the top of the class. Using a property named xCoord, we<br />

can write:<br />

textField.setText(Integer.toString(alien.xCoord));<br />

Suppose also that we want the user to be able to change (set) the value of the x coordinate.<br />

Using the property facility we can write:<br />

alien.xCoord = 56;<br />

The way to provide these facilities is to write a property. Here is the revised class that<br />

includes the property code:<br />

public class AlienWithProperties {<br />

private int x;<br />

private int y;<br />

private int size;<br />

private ImageIcon alienImage;<br />

>

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

Saved successfully!

Ooh no, something went wrong!