Encapsulation is one of the most fundamental concepts of object oriented programming but at the same time one of the most misunderstood. Many developers think of encapsulation as a technique to prevent instance variables from being changed without going through methods. Many think that encapsulation is either supported or not supported by a programming language and if you use a programming language that supports encapsulation then it comes automatically. Neither of these statements is correct. Encapsulation is the technique of isolating the representation of an object and providing access to it through an interface. It's important to understand that your programming language doesn't automatically give you encapsulation. For example, if you have a method in your object that returns a collection that happens to be stored in an instance variable, other code that uses that may be following encapsulation if they read that collection but not if they write to it. Your method could choose to return a copy and that should work fine. If the callers of that method expect to be able to write to that collection and have that change take effect, then they're breaking encapsulation. To see if you have encapsulation, ask yourself "if you could completely change the way you represent the data internally changing all your own methods appropriately would all the users of your class still work properly?" That's the test for true encapsulation.
Download