09OOPEncapsulation - CS108 Stanford Winter 2010 Handout#9...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS108, Stanford Handout #9 Winter 2010 Young OOP Encapsulation Handout written by Nick Parlante OOP Design #1 -- Encapsulation The most basic idea in OOP is that each object encapsulates some data and code. The object takes requests from other client objects, but does not expose the details of its data or code to them. The object alone is responsible for its own state, exposing public messages for clients, and declaring private the ivars and methods that make up its implementation. The client depends on the (hopefully) simple public interface, and does not know about or depend on the details of the implementation. For example, a HashTable object will take get() and set() requests from other objects, but does not expose its internal hash table data structures or the code strategies that it uses. The theme here is one of separation — trying to keep the complexity inside one object from interfering with other objects. Or put another way, trying to keep the various objects as independent from each other as possible (aka "loosely coupled" classes). Each object provides some service or "interface" for the other objects. Ideally, the service is exposed in a way that is simple for the other objects to understand. The complexity of the implementation still exists, but it is isolated inside the implementing class. This works because for most problems there are all sorts of details of the implementation that the clients don't really care about -- how the hash buckets are arranged for example. The interface can capture just the issues relevant to the client, and so be much simpler than the full implementation. Looking at a whole program, we have many objects, each exposing a simple interface to the other objects
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/12/2010 for the course CS 108 taught by Professor Jimenez during the Winter '08 term at Stanford.

Page1 / 2

09OOPEncapsulation - CS108 Stanford Winter 2010 Handout#9...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online