Encapsulation and Abstraction with real time example

Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object-oriented programming methodology, prevents access to implementation details.
Abstraction and encapsulation are related features in object-oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.
Technically
Abstraction : Abstraction is the act of representing essential information without including background details and explanations.
Encapsulation : Encapsulation is the act of wrapping up of attributes(represented by data members) and operations (represented by functions) under one single unit (represented by class).
Taking Real world example
Suppose you go to an automatic cola vending machine and request for a cola. The machine processes your request and gives the cola.
·         Here automatic cola vending machine is a class. It contains both data i.e. Cola can and operations i.e. service mechanism and they are wrapped/integrated under a single unit Cola Vending Machine. This is called Encapsulation.
·         You need not know how the machine is working. This is called Abstraction.
·         You can interact with cola can only through service mechanism. You cannot access the details about internal data like how much cans it contains, mechanism etc. This is Data Hiding.
·         You cannot pick the can directly. You request for cola through proper instructions and request mechanism (i.e. by paying amount and filling request) and get that cola only through specified channel. This is message passing.
The working and data is hidden from you. This is possible because that Vending machine is made (or Encapsulated or integrated) so. Thus, we can say Encapsulation is a way to implement Abstraction.


Share this

Related Posts

Previous
Next Post »