OO Concepts   «Prev  Next»
Lesson 4 Encapsulation
Objective Describe the uses and benefits of encapsulation.

Benefits and Uses of Encapsulation in OO Design

Describe Advantages of OO Encapsulation

As you saw earlier, objects (which are instances of classes) combine both data (also called attributes, variables, and state) with behavior (also called methods, functions, and operations). An object holds information, and can do things with that information on request. The things it can do are the responsibilities of the class. Once you decide that a particular responsibility is assigned to a particular class, you can't change that. Doing so would break other code that is relying on a method's existence and on what parameters it takes. But you can change the inside of your class as much as you like, and the compiler guarantees you will not break any code outside your class by doing so. You'll see more on that later. The inside of your class is generally the information it keeps. OOP keeps related information together, and keeps the methods that work with that information together. That makes your programming much simpler, and allows you to enforce business rules easily, because values can be changed together rather than one at a time. The outside of your class is also called its interface. It's the set of things your class promises to do. Changing your interface risks breaking other code, so once you havfe started to build a system, your interfaces should all be determined and unchangeable.

Encapsulation refers to three things:
  1. The idea of keeping both methods and variables together in a single clump or object
  2. Preventing code outside the object from accessing the inside of the object directly
  3. Creating an outside, or interface, that your class presents to the outside world and that does notchange
Encapsulation frees programmers from having to think about how the inside of a class works. You probably know a way to subtract two dates, but if there's a Date class in your system, you do not need to care: You need to know only that the Date class knows how to subtract one date from another. You need to know how to get the two dates to the Date subtract method and how and where to locate the result. The mechanism is no longer your concern. Other benefits of encapsulation are the ease with which programmers can enforce business rules and the way that changes in your class affect only your class and not the ones around it. You'll see examples of this as the course progresses.

Object-Oriented Design

Using an Object‐oriented Programming paradigm

When using an object‐oriented programming paradigm, objects encapsulate only local data, which is by default accessible only by the object itself. Rather than having to think about data and code as two separate concepts, an object‐oriented program merges the two in the concept of an object. This increases understanding (analysts and programmers can consider objects of interest without internalizing the workings of the complete program) and ease of maintenance.
To realize the latter, object‐oriented programming applies two concepts known as encapsulation and information hiding. One of the greatest sources of errors in programs is when some parts of the program are interfering with other parts. Indeed, it is easy to see that, in this course administration example, the addition of more procedures and data will quickly lead to so‐called spaghetti code, where it becomes very complex to follow the trace of execution as data can jump from one part to another in the program. Object‐oriented programming resolves this issue by encapsulating both data and behavior within an object.

However, this in itself is not sufficient to guarantee maintainable programs, as you also need to prevent an object's data from being directly accessible by other objects. Therefore, object‐oriented programming also emphasizes the concept of information hiding, where an object's data can by default be accessed only by methods contained in the same object. When data elements of one object need to be used by another object, the latter must call a publicly accessible method of the former, basically requesting the owning object to perform a change to its data. As such, object‐oriented programming encourages programmers to place data where it is not directly accessible or modifiable by the rest of the system. Instead, the data is accessible through methods, which can also include checks and safeguards to make sure the requested change is permitted by the owning object.
Object‐oriented programming also defines concepts to help with structuring programs so that they can be easily extended and evolved. These concepts are polymorphism, which is the ability to treat objects of different types in a similar manner, and inheritance, which is a concept to allow for extending objects and enabling code reuse. For now, you will see how the object‐oriented concepts you have seen so far, the
  1. classes,
  2. objects,
  3. variables (data), and
  4. methods (behavior)
are used in C++ and Java.