R – A brilliant example of effective encapsulation through information hiding

abstractionencapsulationinformation-hidinglanguage-agnosticoop

"Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object… encapsulation focuses upon the implementation that gives rise to this behavior… encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics."Grady Booch in Object Oriented Analysis and Design

Can you show me some powerfully convincing examples of the benefits of encapsulation through information hiding?

Best Answer

The example given in my first OO class:

Imagine a media player. It abstracts the concepts of playing, pausing, fast-forwarding, etc. As a user, you can use this to operate the device.

Your VCR implemented this interface and hid or encapsulated the details of the mechanical drives and tapes.

When a new implementation of a media player arrives (say a DVD player, which uses discs rather than tapes) it can replace the implementation encapsulated in the media player and users can continue to use it just as they did with their VCR (same operations such as play, pause, etc...).

This is the concept of information hiding through abstraction. It allows for implementation details to change without the users having to know and promotes low coupling of code.