News for hackers. Stuff that matters.
Re:Inheritance versus Composition by kazinator on Thursday May 09, 2019 @01:04PM (#19870020)
Note that "encapsulation" refers to two completely different things, one of them by mistaken application of the word:

- combination of methods and data

- information hiding

Information hiding is needed for OOP, but it can be hidden in the implementation. The fact that if I have a point object, I can access point.x is not an example of information hiding being violated. Firstly, it doesn't interfere with polymorphism: the same point.x code can access the x coordinate of points of different types. Secondly, the syntax point.x can actually be a method call which invokes an accessor under the hood, so the object doesn't actually have to have a slot x. There is no semantics or "moral" difference between having a pair of methods point.getx() and point.setx(<number>), and just a point.x. Lastly, sometimes it is perfectly acceptable in an object design to have trivial properties.

Information hiding is more important in languages like C++ because an access like "point.x" compiles to code which assumes that x is at a particular offset in that object, and is of a particular type and so forth. It's less important in a dynamic language in which we can cafely ask for the x slot of any object.

Encapsulation of methods and data isn't required in an OOP system; OOP systems like CLOS work without it just fine.

There are no comments on this comment yet.