Hiding details and creating the public interface
The key purpose of modeling an object in object-oriented design is to determine what the public interface of that object will be. The interface is the collection of attributes and methods that other objects can use to interact with that object. They do not need, and are often not allowed, to access the internal workings of the object. A common real-world example is the television. Our interface to the television is the remote control. Each button on the remote control represents a method that can be called on the television object. When we, as the calling object, access these methods, we do not know or care if the television is getting its signal from an antenna, a cable connection, or a satellite dish. We don't care what electronic signals are being sent to adjust the volume, or whether that volume is being output to speakers or a set of headphones. If we open the television to access the internal workings, for example to split the output signal to both external speakers and a set of headphones, we will void the warranty.
This process of hiding the implementation, or functional details, of an object is suitably called information hiding. It is also sometimes referred to as encapsulation, but encapsulation is actually a more all-encompassing term. Encapsulated data is not necessarily hidden. Encapsulation is, literally, creating a capsule, so think of creating a time capsule. If you put a bunch of information into a time capsule, lock and bury it, it is both encapsulated and the information is hidden. On the other hand, if the time capsule has not been buried and is unlocked or made of clear plastic, the items inside it are still encapsulated, but there is no information hiding.
The distinction between encapsulation and information hiding is largely irrelevant, especially at the design level. Many practical references use the terms interchangeably. As Python programmers, we don't actually have or need true information hiding, (we'll discuss the reasons for this in Chapter 2) so the more encompassing definition for encapsulation is suitable.
The public interface, however, is very important. It needs to be carefully designed as it is difficult to change it in the future. Changing the interface will break any client objects that are calling it. We can change the internals all we like, for example, to make it more efficient, or to access data over the network as well as locally, and the client objects will still be able to talk to it, unmodified, using the public interface. On the other hand, if we change the interface, by changing attribute names that are publicly accessed or by altering the order or types of arguments that a method can accept, all client objects will also have to be modified.
Remember, program objects represent real objects, but they are not real objects. They are models. One of the greatest gifts of modeling is the ability to ignore details that are irrelevant. A model car may look like a real 1956 Thunderbird on the outside, but it doesn't run and the driveshaft doesn't turn, as these details are overly complex and irrelevant to the youngster assembling the model. The model is an abstraction of a real concept.
Abstraction is another object-oriented buzzword that ties in with encapsulation and information hiding. Simply put, abstraction means dealing with the level of detail that is most appropriate to a given task. It is the process of extracting a public interface from the inner details. A driver of a car needs to interact with steering, gas pedal, and brakes. The workings of the motor, drive train, and brake subsystem don't matter to the driver. A mechanic, on the other hand works at a different level of abstraction, tuning the engine and bleeding the breaks. Here's an example of two abstraction levels for a car:
Now we have several new terms that refer to similar concepts. Condensing all this jargon into a single sentence, abstraction is the process of encapsulating information with separate public and private interfaces. The private interfaces can be subject to information hiding.
The important thing to bring from all these definitions is to make our models understandable to the other objects that have to interact with them. This means paying careful attention to small details. Ensure methods and properties have sensible names. When analyzing a system, objects typically represent nouns in the original problem, while methods are normally verbs. Attributes can often be picked up as adjectives, although if the attribute refers to another object that is part of the current object, it will still likely be a noun. Name classes, attributes, and methods accordingly. Don't try to model objects or actions that might be useful in the future. Model exactly those tasks that the system needs to perform and the design will naturally gravitate towards one that has an appropriate level of abstraction. This is not to say we should not think about possible future design modifications. Our designs should be open ended so that future requirements can be satisfied. However, when abstracting interfaces, try to model exactly what needs to be modeled and nothing more.
When designing the interface, try placing yourself in the object's shoes and imagine that the object has a strong preference for privacy. Don't let other objects have access to data about you unless you feel it is in your best interest for them to have it. Don't give them an interface to force you to perform a specific task unless you are certain you want them to be able to do that to you.
This is also a good practice for ensuring privacy on your social networking accounts!