DEV Community

Nozibul Islam
Nozibul Islam

Posted on

7 4 4 4 4

Abstraction vs Encapsulation

Let's say a ceiling fan is running. From the outside, we can only see the rotation of the fan, but we cannot see how the motor works inside or how the various components are connected. This hidden part is "Abstraction", which the user cannot directly see or understand.
Now, all the components inside the fan are covered by an outer casing, so they cannot be seen from the outside. This outer layer is "Encapsulation".

Top comments (2)

Collapse
 
tamusjroyce profile image
tamusjroyce

Data models best to OOP

An IS-A joining data = inheritance. Multiple IS-A joins, multiple inheritance (interfaces in some languages facilitate multiple inheritance)

HAS-A relationship = encapsulation

More often we have HAS-A relationships than IS-A relationships

Very, very simple to comprehend

I hate “prefer encapsulation” or “balance inheritance and encapsulation”. There is no rule. Meaning our code becomes inconsistent

Collapse
 
nozibul_islam_113b1d5334f profile image
Nozibul Islam

I have a lot of confusion regarding OOP. I've received different opinions from many experienced engineers, which has only made me more confused. Personally, I don't like Inheritance very much, and I believe the "is-A" relationship has been given too much importance. It's such that many people think the "is-A" relationship should only be through inheritance, but in reality, it depends on how you define it yourself. For example, Admin and User can have an "is-A" relationship with each other, but sometimes they can also have a "has-A" relationship, depending on how we define them differently.

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay