Forem

Nozibul Islam
Nozibul Islam

Posted on

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.