Some object oriented languages (e.g. Smalltalk) do not allow accessing of fields of any other than the current receiver object. For example: expressions like this.good, or this.like:=false would be legal, but expressions like x.like or this.like.good are illegal.
What I don't understand is: why??
What is the rationale for such a restriction?
This is one the the core ideas of OOP called encapsulation. No one except from the object itself knows about it's internal state.
This provides better isolation, as internal state can be changed over the time an if you are accessing it directly - you're screwed. Also if someone can mess with your object's state directly you never know if something will change during a runtime when you don't expect it.
In general it's not hard to define accessors, an in the end you end up with: x like
, x like: false
in smalltalk and x.like()
, x.setLike(false)
in C-like languages. Ruby and Scala allow you to define methods with spaces and invoke them without parenthesis so they look just like field access: x.like
, x.like = false
. You don't have a big overhead if you are forced to write accessors, but if you allow programmers to do everything they want with objects state then you get a chaos in your code, and this is actually a big problem.
To understand all bad things that can happen if you don't use it some time is required. And when you begin to develop, you don't understand what can happen if you keep fields public. That's why C++ is a bad language to start with, as for beginners it's easier to deal with direct field access.
Also if you think about accessing fields directly, then whole idea of OOP is broken. Because you can use any data you have as in procedural languages, and classes then just play a role of grouping functions of defining data structures.
You can read more about encapsulation on wikipedia. Also there is a very interesting post about What is Object Oriented Programming: A Critical Approach