Encapulation is the most poorly understood OO concept.
Academics will quickly tell you that public data fields violate encapsulation and that there is nothing more to it. Engineers will tell you that public data encapsulation is some silly concept they learnt at school. I certainly don't feel I'm going to give the topic justice on this forum - I've even proposed writing a 120 page Dissertation about it (I'll let you know if you like how it goes)! However, encapsulation is of paramount importance in software design. In fact, encapsulation is perhaps the most prevalent metric when determining the quality of software (quality is something the client does not see and is unrelated to the metric of "meeting requirements").
In the context of
Java, consider the following assertions:
- Non-private constructors violate encapsulation
- Non-final classes violate encapsulation (yes that's right - the protected and abstract modifiers are evil!)
- A protected data field violates encapsulation
- A non-private data field of a type that is mutable (such as an array) violates encapsulation.
- All violations of encapsulation are of the same magnitude - you can't "violate it more this way than that way"
- The lack of access modifiers means that encapsulation cannot be fully enforced at times (C# makes efforts to fix this)
- The Java language intrinsically violates encapsulation since it forces the inheritance of implementation (specifically, java.lang.Object).
I realise these assertions (which by the way, are incomplete) will raise more questions than provide answers, but I do hope to one day give them the full explanation that they deserve.
[ April 06, 2005: Message edited by: Tony Morris ]