Pages

Saturday, February 27, 2016

Difference between Abstraction and Encapsulation

One of the most confusing concept for developers to understand is the difference between abstraction and encapsulation. This is because the definition seem to say the same thing using different words.

Abstraction is defined as the process of  generalization thus showing only what is necessary.
Encapsulation on the other hand is defined as process of hiding the unnecessary details.

A real world example of abstraction and encapsulation is TV and a remote.

TV hides the complex circuitry inside it. This is similar to encapsulation. Remote provides us an interface to operate the TV without knowing the internal details. This is similar to abstraction.

In programming, encapsulation is achieved by using getters , setters and access modifiers. Also, every method is encapsulation as it hides the internal details.

Abstraction is achieved by using abstract classes and interfaces. They provide a common implementation which is used by derived classes thus providing generalization.


No comments:

Post a Comment