What is difference between abstraction and encapsulation?

OmkarMozar
OmkarMozar
Published in
2 min readJan 18, 2021

--

Well in my opinion abstraction is getting interested from reality .

In abstraction, We hide something to reduce the complexity of it and In encapsulation, We hide something to protect the data.

So we define encapsulation as wrapping of data and methods in single entity referred as class.

In java we achieve encapsulation using getters and setters not just by wrapping data and methods in it. we also define a way to access that data. and while accessing data we protect it also.

Technical e.g would be to define a private data variable called weight. Now we know that weight can’t be zero or less than zero in real world scenario.
Imagine if there are no getters and setters someone could have easily set it to a negative value being public member of class.

Now final difference using one real world example,
Consider a circuit board consisting of switches and buttons. We wrap all the wires into a a circuit box, so that we can protect someone by not getting in contact directly(encapsulation).

We don’t care how those wires are connected to each other we just want an interface to turn on and off switch. That interface is provided by buttons(abstraction)

Abstraction :-

Various calculations: Addition, Multiplication, Subtraction, Division, Square, Sin, Cos, Tan.

We do not show the details of how do we calculate the Sin, Cos or Tan. We just Show Calculator and it’s various Methods which will be, and which needs to be used by the user.

Encapsulation :-

Instead of calling it as Addition, Subtraction, Multiplication, Division, Now we will call it as a Calculator.

--

--