3 Encapsulation, data abstraction and information hiding

Encapsulation, data abstraction and information hiding all boil down to the same thing: a published and well defined ``interface'' for a particular data type. As discussed in module 0033, the built-in type int is a good example. This is because the ``interface'' to use an int type variable is published and well defined. Operators that work on int variables include +, -, *, /, %, =, ==, != and etc.

However, encapsulation (along with data abstraction and information hiding) also implies least privilege exposure. This means the interface to a data type does not expose anything more than the consumer needs to know.

The main purpose of encapsulation is to insulate consumers from changes from the provider's point of view. Using a television analogy, we don't want consumers to relearn how to operate a television just because technology has changed (from CRT to LCD, for example). A well designed television remote control insulates a consumer from the actual implementation of a television.

In the case of programming, insulation from implementation means that changes done to the implementation of a data type has no impact to the rest of the program.

Copyright © 2006-09-07 by Tak Auyeung