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.

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

Often, programmers do not have <strong>to</strong> create classes “from scratch.” Rather, they can derive<br />

classes from other classes that provide behaviors required by the new classes. Classes also can<br />

include references <strong>to</strong> objects of other classes as members. Such software reuse can greatly<br />

enhance programmer productivity. Chapter 9 discusses inheritance—the process by which<br />

new classes are derived from existing classes. Section 8.8 discusses composition (aggregation),<br />

in which classes include as members references <strong>to</strong> objects of other classes.<br />

8.3 Class Scope<br />

In Section 6.11, we discussed method scope; now, we discuss class scope. A class’s instance<br />

variables and methods belong <strong>to</strong> that class’s scope. Within a class’s scope, class<br />

members are accessible <strong>to</strong> all of that class’s methods and can be referenced by name. Outside<br />

a class’s scope, class members cannot be referenced directly by name. Those class<br />

members that are visible (such as Public members) can be accessed only through a “handle”<br />

(i.e., members can be referenced via the format objectReferenceName.memberName).<br />

If a variable is defined in a method, only that method can access the variable (i.e., the<br />

variable is a local variable of that method). Such variables are said <strong>to</strong> have block scope. If<br />

a method defines a variable that has the same name as a variable with class scope (i.e., an<br />

instance variable), the method-scope variable hides the class-scope variable in that<br />

method’s scope. A hidden instance variable can be accessed in a method by preceding its<br />

name with the keyword Me and the dot opera<strong>to</strong>r, as in Me.mHour. We discuss keyword<br />

Me later in this chapter.<br />

8.4 Controlling Access <strong>to</strong> Members<br />

The member access modifiers Public and Private control access <strong>to</strong> a class’s instance<br />

variables and methods. (In Chapter 9, we introduce the additional access modifiers Protected<br />

and Friend.)<br />

As we stated previously, Public methods serve primarily <strong>to</strong> present <strong>to</strong> the class’s clients<br />

a view of the services that the class provides (i.e., the Public interface of the class).<br />

We have mentioned the merits of writing methods that perform only one task. If a method<br />

must execute other tasks <strong>to</strong> calculate its final result, these tasks should be performed by a<br />

utility method. A client does not need <strong>to</strong> call these utility methods, nor does it need <strong>to</strong> be<br />

concerned with how the class uses its utility methods. For these reasons, utility methods are<br />

declared as Private members of a class.<br />

Common <strong>Program</strong>ming Error 8.2<br />

Attempting <strong>to</strong> access a Private class member from outside that class is a syntax error. 8.2<br />

The application of Fig. 8.3 demonstrates that Private class members are not accessible<br />

outside the class. Line 9 attempts <strong>to</strong> access Private instance variable mHour of<br />

CTime object time. The compiler generates an error stating that the Private member<br />

mHour is not accessible. [Note: This program assumes that the CTime class from Fig. 8.1<br />

is used.]<br />

Good <strong>Program</strong>ming Practice 8.5<br />

We prefer <strong>to</strong> list instance variables of a class first, so that, when reading the code, programmers<br />

see the name and type of each instance variable before it is used in the methods of the class. 8.5

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

Saved successfully!

Ooh no, something went wrong!