Question

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?

Was it helpful?

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.

OTHER TIPS

Low encapsulation allows tight coupling.

Encapsulation protects implementation details of classes from external access. External access is coupling. Coupling to implementation details is tighter than coupling to a public API -- there are more connections among parts of the code to be written and understood and managed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top