Skip to main content

Posts

Showing posts with the label encapsulation

Encapsulation and information hiding

Encapsulation and information hiding are often used interchangeably. But they are actually different. Information hiding is a design principle, whereas encapsulation is a langauge facility. Encapsulation is the process of bundling code and the data it acts on together in one entity. David Parnas describes information hiding as "hiding of critical design decisions", so that the client code does not have to understand the intricacies of the design, and is also oblivious to any changes in design. Encapsulation can happen without information hiding, but will not be effective. Encapsulation facilitates, but does not gaurantee information hiding. Following some simple guidelines will help us create better classes   Rules for encapsulation: Rules for information hiding: These rules have been taken from this JavaWorld article on encapsulation . It's an excellent article that explains the concepts very well with relevant and well thought of code samples. Something on the side...

Encapsulation

Encapsulation and information hiding are often used interchangeably. But they are actually different. Information hiding is a design principle, whereas encapsulation is a language facility. Encapsulation is the process of bundling code and the data it acts on together in one entity. David Parnas describes information hiding as "hiding of critical design decisions", so that the client code does not have to understand the intricacies of the design, and is also oblivious to any changes in design. Encapsulation can happen without information hiding, but will not be effective. Encapsulation facilitates, but does not guarantee information hiding. Following some simple guidelines will help us create better classes Encapsulation rule 1: Place data and the operations that perform on that data in the same class Encapsulation rule 2: Use responsibility-driven design to determine the grouping of data and operations into classes Information hiding rule 1: Don't expose data items In...