Abstraction and Encapsulation

We have seen many people struggling with the concept of Abstraction and Encapsulation. So this is our take on this to help you understand it well.

cheezycode-abstraction-encapsulation



What is Abstraction?


- Ignoring those aspects of an object that are not relevant to the current scope of the problem.
- Reduces scope and helps managing complexity.

Let's take an example to make it more clear - You are designing a website to sell new cars and you must be having a class named Car. You need to decide the attributes that are required to represent the Car object. Say, you decide upon these attributes - Description, Type, Price and so on. Our current scope is selling a new car -

1. Do I need to include Service History as a field? No (because it does not belong to a new car and is not relevant to the current scope).

2. Do I need to include Vehicle Registration Number as a field? No (because currently we do not have and it’s not necessary in our current scope. You get the number after you buy the car.)

So we choose only those properties that are relevant to our current scope, we exclude every other property that is not relevant. This reduces complexity as we only focus on what is required.

EXCLUDE WHICH IS
NOT RELEVANT TO THE CURRENT SCOPE

What is Encapsulation?


- Provides interface to access the functionality of the object & hides how it is implemented (Information Hiding).
- Keep the attributes and behavior as one unit – Helps to make the module more independent.

Let's take an example of this as well - consider a keyboard, you know that when you press A, it will be printed on the screen, you don't know how it is implemented. You just have a set of keys as part of a keyboard interface - exposed to you but its implementation is hidden from you. This is encapsulation. You as a user do not know how it is implemented, you just know the interface to use it properly. What is the benefit of hiding its implementation and exposing only the interface -

1. Implementation can be changed easily if you find any other solution that works well.
2. Maintenance is easy - you have a clear defined interface exposed, you can tweak the functionality easily but interface will remain same.

Other aspect of Encapsulation i.e. to keep the attributes and behavior together. As a programmer, you know that when you press A - 65 ASCII value is passed as a code or a signal. Keyboard object has this data as private. Only internal methods have access to it. This helps in having some variables as private as we don't want others to access it or modify it. Those who need to access, only those are allowed and are kept along with the data itself. So they are treated as one unit and encapsulated.  


Conclusion (Abstraction Vs Encapsulation)


Abstraction comes before Encapsulation in SDLC. In abstraction, we are trying to come up with the scope of the problem whereas in encapsulation, we are trying to come up with the approach to solve and implement problem in a better way. You can also check out this video for this post.





Let us know if you have any query or concerns. Happy Reading.


Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example