Web server setup - 388 Object-Based Programming Chapter 8 usually designed to
Friday, April 27th, 2007388 Object-Based Programming Chapter 8 usually designed to be client oriented rather than implementation oriented. Clients need not be concerned with a class s implementation. Interfaces do change, but less frequently than implementations. When an implementation changes, implementation-dependent code must change accordingly. By hiding the implementation, we eliminate the possibility of other program parts becoming dependent on the details of the class implementation. Often, classes do not have to be created from scratch. Rather, they can be derived from other classes that provide operations the new classes can use, or classes can include objects of other classes as members. Such software reuse can greatly enhance programmer productivity. Deriving new classes from existing classes is called inheritance a distinguishing feature between object-based programming and object-oriented programming and is discussed in detail in Chapter 9. Including class objects as members of other classes is called composition or aggregation and is discussed later in this chapter. 8.3 Class Scope A class s instance variables and methods belong to that class s scope. Within a class s scope, class members are accessible to all of that class s methods and can be referenced simply by name. Outside a class s scope, class members cannot be referenced directly by name. Those class members (such as public members) that are visible can be accessed only through a handle (i.e., primitive data type members can be referred to by object- ReferenceName.primitiveVariableName and object members can be referenced by object- ReferenceName.objectMemberName). For example, a program can determine the number of elements in an array object by accessing the array s public member length as in arrayName.length. Variables defined in a method are known only to that method (i.e., they are local variables to that method). Such variables are said to have block scope. If a method defines a variable with the same name as a variable with class scope (i.e., an instance variable), the class-scope variable is hidden by the method-scope variable in the method scope. A hidden instance variable can be accessed in the method by preceding its name with the keyword this and the dot operator, as in this.variableName. Keyword this is discussed Section 8.13. 8.4 Controlling Access to Members The member access modifiers public and private control access to a class s instance variables and methods. (In Chapter 9, we will introduce the additional access modifier protected.) As we stated previously, the primary purpose of public methods is to present to the class s clients a view of the services the class provides (i.e., the public interface of the class). Clients of the class need not be concerned with how the class accomplishes its tasks. For this reason, the private instance variables and privatemethods of a class (i.e., the class s implementation details) are not accessible to the clients of a class. Restricting access to class members via keyword private is called encapsulation. Common Programming Error 8.3 An attempt by a method that is not a member of a particular class to access a private member of that class is a syntax error. Figure 8.3 demonstrates that private class members are not accessible by name outside the class. Line 10 attempts to access directly the private instance variable hourof Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services