0.1.9 Encapsulation

Encapsulation is one of the four pillars that object-oriented program is created on. The main objective of encapsulation is to hide information. If you use a machine like an ATM, you may not have any idea of what happens behind the scenes, you may know how to use the machine and the results of it - There are inputs through the use of one (pressing buttons, going through menus) and there are outputs as a result of one being used (receiving money). The user of the ATM (usually) has now idea how the ATM works. There are inputs and outputs, but what happens in between is not relevant to the user.

Another example would be that of a hot dog. Many people like hot dogs, so they buy and eat them regularly. To the end user they taste good, so why wouldn’t they? How the hot dog is originally made, processed, packaged is irrelevant to the end user. Another easy example would be that of a car. You press the gas and press the brake and go faster and slower respectively. Somebody that drives every day will do these things and may not know how they work. If you do one of these things and what you expect to happen does not happen, you call a mechanic. From a consumer perspective, all that happens is that you are able to put a key in a car, start it, stop it etc. and that’s all that matters to them.

One final example would be that of a computer. You interact with it, turn it on then do as you please. The behavior of the computer and what happens behind the scenes is encapsulated from the user. It is used as a tool.