Abstraction vs Encapsulation
Nozibul Islam

Nozibul Islam @nozibul_islam_113b1d5334f

About: I am a Full-Stack Developer specialized Front-end Developer. Passionate about algorithms, data structures, and coding challenges & always ready to face new challenges.

Location:
Dhaka, Bangladesh
Joined:
Aug 24, 2024

Abstraction vs Encapsulation

Publish Date: Feb 16
23 2

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".

Comments 2 total

  • tamusjroyce
    tamusjroyceFeb 17, 2025

    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

    • Nozibul Islam
      Nozibul IslamFeb 18, 2025

      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.

Add comment