Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A brilliant example of effective encapsulation through information hiding?

"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?

like image 812
Roberto Russo Avatar asked Mar 11 '09 18:03

Roberto Russo


People also ask

What is the best example of encapsulation?

Consider the below real time example: Encapsulation: As a driver you know how to start the car by pressing the start button and internal details of the starting operations are hidden from you. So the entire starting process is hidden from you otherwise we can tell starting operation is encapsulated from you.

What is the example of encapsulation?

In general, encapsulation is a process of wrapping similar code in one place. In C++, we can bundle data members and functions that operate together inside a single class. For example, class Rectangle { public: int length; int breadth; int getArea() { return length * breadth; } };

What is encapsulation explain with real time example?

Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines. We can create a fully encapsulated class in Java by making all the data members of the class private.

Can you give a real world example of encapsulation and abstraction?

I can think of the interaction between a user and a mobile phone as an example of encapsulation. The user does not need to understand the internal workings of the phone in order to utilise it, which is referred to as abstraction.


2 Answers

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.

like image 132
Ben S Avatar answered Sep 23 '22 10:09

Ben S


The *nix abstraction of character streams (disk files, pipes, sockets, ttys, etc.) into a single entity (the "everything is a file") model allows a wide range of tools to be applied to a wide range of data sources / sinks in a way that simply would not be possible without the encapsulation.

Likewise, the concept of streams in various languages, abstracting over lists, arrays, files, etc.

Also, concepts like numbers (abstracting over integers, half a dozen kinds of floats, rationals, etc.) imagine what a nightmare this would be if higher level code was given the mantissa format and so forth and left to fend for itself.

like image 27
MarkusQ Avatar answered Sep 19 '22 10:09

MarkusQ