Abstraction means providing the essential features without showing its inner details or hiding internal implementation
Abstraction : you'll never buy a "device", but always buy something more specific : iPhone, GSII, Nokia 3310... Here, iPhone, GSII and N3310 are concrete things, device is abstract.
Encapsulation : you've got several devices, all of them have got an USB port. You don't know what kind of printed circuit there's back, you just have to know you'll be able to plug an USB cable onto.
Abstraction is a concept, which is allowed by encapsulation. My exemple wasn't the best one (there's no real link between the two blocks).
You can do encapsulation without using abstraction, but if you wanna use some abstraction in your projets, you'll need encapsulation.
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!!
No comments:
Post a Comment