created 12/13/98; revised 09/23/99, 01/21/00, 07/14/02

CHAPTER 33 — Encapsulation

So far, the objects we have designed have all of their methods and variables visible to any part of the program that has a reference to the object. Thus, code other than a method of an object can change an object's instance variable. This is often unwise.

A better design enforces encapsulation. Some (or all) of an object's instance variables are visible only to the object's own methods. Code outside of the object cannot access these variables directly. This chapter discusses how to use the private visibility modifier to do this.

Chapter Topics:


Should a bank control what software has access to your checking account?