I have not worked in Java but I used to do in when I was doing my graduation. One thing I always liked about Java is encapsulation. It provides really good encapsulation in terms of readability. I don’t know if following code will work or not but this is prototype of encapsulation in Java
But it does not give you the notion of Encapsulation which you get while coding in Java. So Coffeescript comes to rescue. Take a look at gist below
First person is defined as a variable which assures that person will be accessible anywhere in that scope. Now on line 2 it has been assigned a self executing anonymous function (something which you don’t see in traditional languages). Then inside local instance of person is created and further snippet is exactly same as I have shown in snapshot earlier. In this scope person acts as a class (as its prototype is defined) and same person object is assigned to person from outer scope with help of return which makes outer person closure. Since person is now a closure you can still access doPrivateThings by maintaining the context.
It is not great in terms of readability but it is intricate and brain teasing. I don’t know how many of you actually understood the concept here as it is really hard to understand in one read.
I found a good thread on stack overflow about to it in which another method is described which can do encapsulation.