The last member of Object oriented principles family is “Encapsulation”, this fourth member of the family is my most favorite one, I love it because it seems Mafias rules -actually I am not a member of Mafia but I am absorbed in movies about this gang- Based on this rule “The less you know the more safe you are” or in more technical words “No part of a complex system should depend on the internal details of any other part”. Thus in the object oriented world a class has to try to hide information about its internal structure and avoid depending on other classes internal structure, Here the “structure” means Algorithm, the order of methods calls, required settings before calling a method and etc.
As Grady Booch States in his seminal book –Object oriented analysis and design with applications-
«Abstraction and encapsulation are complementary concepts: Abstraction focuses on the observable behavior of an object, whereas encapsulation focuses on the implementation that gives rise to this behavior.Encapsulation is most often achieved through information hiding (not just data hiding), which is the process of hiding all the secrets of an object that do not contribute to its essential characteristics;” Whereas abstraction “helps people to think about what they are doing,” encapsulation “allows program changes to be reliably made with limited effort” Encapsulation provides explicit barriers among different abstractions and thus leads to a clear separation of concerns.In practice, this means that each class must have two parts: an interface and an implementation. The interface of a class captures only its outside view, encompassing our abstraction of the behavior common to all instances of the class. The implementation of a class comprises the representation of the abstraction as well as the mechanisms that achieve the desired behavior. The interface of a class is the one place where we assert all of the assumptions that a client may make about any instances of the class; the implementation encapsulates details about which no client may make assumptions. To summarize, we define encapsulation as follows:
Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation.»