Encapsulation And Abstraction, A Real Time Example!
Sign in

Encapsulation and Abstraction, a real time example!

Sr.Software Developer
  Most of the time we used to hear these two main OOP's concepts i.e. Abstraction and Encapsulation. Whenever I read about this two main OOP's concepts in any of the book or blog, I used to get some predefined definitions and failed to get the real meaning. But recently I got a very good real time example and I think this will help you guys.

Encapsulation definition from Wiki: "Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So the public methods like getter and setter access it and the other classes call these methods for accessing".

Abstraction definition from Wiki:"Abstraction involves the facility to define objects that represent abstract "actors" that can perform work, report on and change their state, and "communicate" with other objects in the system."

So after reading this wiki, can you make out something? I think the answer will be a big NO!!(If Yes, please close this window, because you are already a Geek :-P )

Consider the below real time example:

Let’s imagine you own a Ferrari Car and you are the only one knows how to drive it in your family. One day a terrible breakdown happened to your car and you bring one mechanic to home and he checked it. But he is unable to repair it. So you contacted Ferrari company and some chief mechanic came to your home and repaired it(Since your car is under warranty, your pocket is still big :-))This is a real time example for the above mentioned OOP's concepts, How?

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.

OR

The driving wheel is encapsulated the process of rotating the wheel from you.

Abstraction:

Before mentioning anything about abstraction, we can take three different users here (I am calling them as entity)

1) You 2) Local Mechanic 3) Expert

You Entity: Since you know only to start the car by pressing a button and all other operations behind the scene are abstracted from you.

Local Mechanic Entity: Our local mechanic knows some of the implementation of starting the car, i.e. he can open car's bonnet and check the battery cable or chock etc. So in short Local Mechanic Entity knows some of the implementations of the car.

Expert Entity: Since our expert (Designer of the car) mechanic knows all the operations of our car, he can repair it very quickly. So in short Expert Entity knows all the implementations of the car.

The car's operation is completely abstracted from you and it is partially implemented to Local Mechanic Entity and fully implemented to Expert Entity. So you are an abstract class having only abstract methods, Local Mechanic Entity has extended You(Since he is also an ordinary user) and he implemented some of the methods and last our expert Entity extending Local Mechanic and implementing all the methods.

Also in terms of complexity "Whenever abstraction decreases, complexity increases"(Since our Expert Entity has very less abstraction, his complexity of work also increases)

In Java, garbage collection is also a best example for explaining abstraction.

As a java programmer GC is abstracted completely from you and as a language designer, one needs to understand the internal details of the JVM.

Hope this Helps!!

start_blog_img