EarthWeb
Developer.com
Access FREE Novell Tools:
Tool: Online TCO Calculator
See how migrating from Solaris to SUSE* Linux Enterprise Server from Novell* delivers all the reliability, performance and scalability of Solaris for up to 75% less.
Whitepaper: The Trend from UNIX to Linux in SAP Data Centers
Realize high platform-related cost savings in a business-critical SAP landscape with high-availability requirements by moving to Linux.
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-
Bruce Eckel's Thinking in Java Contents | Prev | Next

5: Hiding the implementation

A primary consideration in object-oriented design is “separating the things that change from the things that stay the same.”

This is particularly important for libraries. The user ( client programmer ) of that library must be able to rely on the part they use, and know that they won’t need to rewrite code if a new version of the library comes out. On the flip side, the library creator must have the freedom to make modifications and improvements with the certainty that the client programmer’s code won’t be affected by those changes.

This can be achieved through convention. For example, the library programmer must agree to not remove existing methods when modifying a class in the library, since that would break the client programmer’s code. The reverse situation is thornier, however. In the case of a data member, how can the library creator know which data members have been accessed by client programmers? This is also true with methods that are only part of the implementation of a class, and not meant to be used directly by the client programmer. But what if the library creator wants to rip out an old implementation and put in a new one? Changing any of those members might break a client programmer’s code. Thus the library creator is in a strait jacket and can’t change anything.

To solve this problem, Java provides access specifiers to allow the library creator to say what is available to the client programmer and what is not. The levels of access control from “most access” to “least access” are public, “friendly” (which has no keyword), protected, and private. From the previous paragraph you might think that, as a library designer, you’ll want to keep everything as “private” as possible, and expose only the methods that you want the client programmer to use. This is exactly right, even though it’s often counterintuitive for people who program in other languages (especially C) and are used to accessing everything without restriction. By the end of this chapter you should be convinced of the value of access control in Java.

The concept of a library of components and the control over who can access the components of that library is not complete, however. There’s still the question of how the components are bundled together into a cohesive library unit. This is controlled with the package keyword in Java, and the access specifiers are affected by whether a class is in the same package or in a separate package. So to begin this chapter, you’ll learn how library components are placed into packages. Then you’ll be able to understand the complete meaning of the access specifiers.

Access FREE Intel Whitepapers and Briefs at the Intel Resource Center
Whitepaper:
Reduce Maintenance Costs While Increasing Notebook Security

Mobility Whitepaper:
Increase Performance, Security with Intel Centrino Pro Processor Technology

Servers/Workstations Whitepaper:
Itanium 2-based Solutions and x86 Architecture

Servers/Workstations Whitepaper:
Which Is Right for You--Itanium 2 or x86 Architecture?

Clients Demo:
Take Business PCs to the Next Level

internet.commerce
Partner With Us
Calling Cards
Web Design
Promotional Items
Promotional Pens
KVM Switches
KVM over IP
Baby Photo Contest
Find Software
Promotional Golf
Home Improvement
Imprinted Gifts
Online Universities
Auto Insurance Quote
Prepaid Phone Card



Acceptable Use Policy

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers