Search code examples
javaencapsulationtightly-coupled-code

How can you get unnecessarily tight coupling if you have too low encapsulation


I can't seem to grab the concept of "tight coupling" is brought if I have low encapsulation in my code.

Will somebody please show some "before" and "after" code that illustrates this concept?


Solution

  • If you don't encapsulate your fields and other should be private members, they may be accessed directly from other classes. If the accessed item is implementation dependent, changing the implementation triggers the need of changing the code everywhere it was referenced.

    E.g if you have a class representing a date and you switch from the year, month, day representation to milliseconds representation (and a method actually transforming this into a human readable format), if the fields are exposed, then this will cause a trouble.