Hi @mtamazlicaru, sorry for the very late response. I've been away from dev.to for a while.
This behavior is part of the encapsulation principle in object-oriented programming. Encapsulation is about keeping a class's internal data and implementation details 'enclosed' within the class, and only exposing a public interface to the outside world.
However, a unique aspect of this principle is that in many programming languages, two instances of the same class can access each other's private methods and properties. This is because the class itself is considered the boundary of encapsulation, not the individual instances.
So, when two objects are of the same class, they are allowed to see each other's private parts, so to speak. This feature can be quite useful in certain scenarios, like implementing methods that compare two objects of the same class. Hope this helps clarify things!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yes, that is part of OO principles.
Hi, thanks for the article. Could you please specify what OO principle is this?
Hi @mtamazlicaru, sorry for the very late response. I've been away from dev.to for a while.
This behavior is part of the encapsulation principle in object-oriented programming. Encapsulation is about keeping a class's internal data and implementation details 'enclosed' within the class, and only exposing a public interface to the outside world.
However, a unique aspect of this principle is that in many programming languages, two instances of the same class can access each other's private methods and properties. This is because the class itself is considered the boundary of encapsulation, not the individual instances.
So, when two objects are of the same class, they are allowed to see each other's private parts, so to speak. This feature can be quite useful in certain scenarios, like implementing methods that compare two objects of the same class. Hope this helps clarify things!