30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 Object-Based <strong>Program</strong>ming 305<br />

Note that the program declares instance variables mHour, mMinute and mSecond<br />

as Private. Instance variables declared Private are not accessible outside the class in<br />

which they are defined. The class’s clients are not concerned with the actual data representation<br />

of that class. For example, the class could represent the time internally as the number<br />

of seconds that have elapsed since the previous midnight. Suppose this representation<br />

changes. Clients still are able <strong>to</strong> use the same Public methods and obtain the same results<br />

(Return values) without becoming aware of the change in internal representation. In this<br />

sense, the implementation of a class is said <strong>to</strong> be hidden from its clients.<br />

Software Engineering Observation 8.5<br />

Information hiding promotes program modifiability and simplifies the client’s perception of<br />

a class. 8.5<br />

Software Engineering Observation 8.6<br />

Clients of a class can (and should) use the class without knowing the internal details of how<br />

the class is implemented. If the class implementation is changed (<strong>to</strong> improve performance,<br />

for example), provided that the class’s interface remains constant, the class clients’ source<br />

code need not change. This makes it much easier <strong>to</strong> modify systems. 8.6<br />

In this program, the CTime construc<strong>to</strong>r initializes the instance variables <strong>to</strong> 0 (i.e., the<br />

universal time equivalent of 12 AM) <strong>to</strong> ensure that the object is created in a consistent state<br />

(i.e., all instance variable values are valid). The instance variables of a CTime object<br />

cannot s<strong>to</strong>re invalid values, because the construc<strong>to</strong>r (which calls SetTime) is called when<br />

the CTime object is created. Method SetTime scrutinizes subsequent attempts by a client<br />

<strong>to</strong> modify the instance variables.<br />

Normally, instance variables are initialized in a class’s construc<strong>to</strong>r, but they also can<br />

be initialized when they are declared in the class body. If a programmer does not initialize<br />

instance variables explicitly, the compiler initializes them. When this occurs, the compiler<br />

sets primitive numeric variables <strong>to</strong> 0, Booleans <strong>to</strong> False and references <strong>to</strong> Nothing).<br />

Methods ToUniversalString and ToStandardString take no arguments<br />

because, by default, these methods manipulate the instance variables of the particular<br />

CTime object for which they are invoked. This makes method calls more concise than<br />

conventional function calls in procedural programming. It also reduces the likelihood of<br />

passing the wrong arguments, the wrong types of arguments or the wrong number of<br />

arguments.<br />

Software Engineering Observation 8.7<br />

The use of an object-oriented programming approach often simplifies method calls by reducing<br />

the number of parameters that must be passed. This benefit of object-oriented<br />

programming derives from the fact that encapsulation of instance variables and methods<br />

within an object gives the object’s methods the right <strong>to</strong> access its instance variables. 8.7<br />

Classes simplify programming, because the client (or user of the class object) need be<br />

concerned only with the Public operations encapsulated in the object. Usually, such<br />

operations are designed <strong>to</strong> be client-oriented, rather than implementation-oriented. Clients are<br />

neither aware of, nor involved in, a class’s implementation. Interfaces change less frequently<br />

than do implementations. When an implementation changes, implementation-dependent code<br />

must change accordingly. By hiding the implementation, we eliminate the possibility that<br />

other program parts will become dependent on the class-implementation details.

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

Saved successfully!

Ooh no, something went wrong!