Controlling Access to Members

The access modifiers public and private control access to a class's variables and methods. (In Chapter 9, we will introduce the additional access modifier protected.) As we stated in Section 8.2, the primary purpose of public methods is to present to the class's clients a view of the services the class provides (the class's public interface). Clients of the class need not be concerned with how the class accomplishes its tasks. For this reason, the private variables and private methods of a class (i.e., the class's implementation details) are not directly accessible to the class's clients.

Figure 8.3 demonstrates that private class members are not directly accessible outside the class. Lines 911 attempt to access directly the private instance variables hour, minute and second of the Time1 object time. When this program is compiled, the compiler generates error messages stating that these private members are not accessible. [Note: This program assumes that the Time1 class from Fig. 8.1 is used.]

Figure 8.3. Private members of class Time1 are not accessible.

 1 // Fig. 8.3: MemberAccessTest.java
 2 // Private members of class Time1 are not accessible.
 3 public class MemberAccessTest
 4 {
 5 public static void main( String args[] )
 6 {
 7 Time1 time = new Time1(); // create and initialize Time1 object
 8
 9 time.hour = 7; // error: hour has private access in Time1 
10 time.minute = 15; // error: minute has private access in Time1
11 time.second = 30; // error: second has private access in Time1
12 } // end main
13 } // end class MemberAccessTest
 
MemberAccessTest.java:9: hour has private access in Time1
 time.hour = 7; // error: hour has private access in Time1
 ^
MemberAccessTest.java:10: minute has private access in Time1
 time.minute = 15; // error: minute has private access in Time1
 ^
MemberAccessTest.java:11: second has private access in Time1
 time.second = 30; // error: second has private access in Time1
 ^
3 errors
 

Common Programming Error 8.1

An attempt by a method that is not a member of a class to access a private member of that class is a compilation error.


Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net