Concepts / Encapsulation and Data Hiding

Encapsulation and Data Hiding

Procedure-oriented programming centers on functions that manipulate data, keeping data and logic separate; object-oriented programming bundles data and behavior together into objects.

  • Programming

Two Ways to Organize a Program

Every program must answer a basic design question: should the code be organized around functions that process data, or around entities that bundle data with the operations performed on that data? These choices represent procedure-oriented and object-oriented programming. The choice affects how you think about the problem and how the program can be maintained as it grows.

Key point

Procedural thinking begins with operations and asks, What steps must the program perform? Object-oriented thinking begins with entities and asks, What entities exist, what state do they have, and what can they do?

Following Data Through Two Designs

Imagine that a program must work with some data. In a procedure-oriented design, functions are the center of organization: they receive or locate data and manipulate it. The data and the logic that operates on it remain separate. In an object-oriented design, the data and the methods that operate on that data are brought together inside objects.

FunctionsoperationsObjectdata and behaviorDataobject stateDataseparate from logicMethodsoperations on datamanipulatecontainscontainsoperates on
Where does data movement and control occur in a procedure-oriented design compared with an object-oriented design?
Worked example

A Generated Design Sketch

Organize a program that must work with the state of several accounts.

  1. 1
    Procedure-oriented view

    Begin with functions that perform operations on account data. The functions and the data they manipulate are separate parts of the design.

  2. 2
    Object-oriented view

    Begin with account entities. Each object combines account data with the behavior that operates on that data.

  3. 3
    Design question

    Ask whether the problem is mainly a sequence of operations or whether it naturally contains entities with their own state and behavior.

Result

The same general problem can be organized around functions and separate data or around objects that bundle data and behavior. The appropriate choice depends on the problem rather than on a rule that one paradigm is always best.

Bundling State and Behavior

Encapsulation is the object-oriented idea of keeping an object's data together with the methods that operate on that data. Instead of treating data as one separate part of a program and functions as another, the design presents them as one object. This gives the object both state, represented by its data, and behavior, represented by its methods.

Dataseparate elementObjectbundled designDatastateFunctionsseparate elementMethodsbehaviorbundlesbundles
How are an object's data and the functions that operate on that data connected, and how is this different from keeping data and functions separate?
Key point

Encapsulation does not merely mean putting related names in one place. It changes the organizing unit of the program: the object becomes the place where relevant data and the operations on that data are considered together.

Keeping Internal Data Controlled

Data hiding is the access-focused side of this design. When data and its behavior belong to an object, code outside the object can be designed to use the object's behavior rather than treating the internal data as a separate resource to manipulate directly. The important boundary is between the object's internal data and the behavior through which the object is used.

Outside codeuses behaviorBehavioroperationsObjectboundaryInternal datastateusesoperates oncontainscontains
What can code outside an object use directly, and how does it work with the object's internal data through the object's behavior?

Templates and Instances

A class is a template that defines the structure and behavior of objects. An object is a specific instance of a class with actual values. One class can therefore describe a common design while separate objects contain their own values and follow that shared structure and behavior.

Classstructure and behaviorObject Aactual valuesObject Bactual valuesinstance ofinstance of
How does one class define a shared design while multiple objects contain different actual values?
Worked example

The Integer Class

Relate the idea of a class template to integer values.

  1. 1
    Template

    The int class defines how integers behave and what operations can be performed on them.

  2. 2
    Instance

    When an integer variable is created in Python, it is an object that is an instance of the int class.

  3. 3
    Separate values

    Each integer variable is a separate object with its own value, while all such objects follow the int class template.

Result

The class provides the shared design, while each object is a particular instance with an actual value.

Choosing a Paradigm

Neither paradigm is always the correct choice. Procedural programming is often simpler to write initially and works well for small to medium programs, especially when the problem naturally decomposes into a sequence of operations. Object-oriented programming scales better for large, complex systems, especially when the problem naturally decomposes into entities with their own state and behavior.

QuestionProcedure-oriented thinkingObject-oriented thinking
What is the main organizing unit?Functions that manipulate dataObjects that bundle data and behavior
How is the problem viewed?As a sequence of operationsAs entities with state and behavior
When is it often suitable?Small to medium, straightforward programsLarge, complex systems
What is the main trade-off?Initial simplicityGreater upfront complexity in exchange for an approach that can scale better

A practical comparison of the two design philosophies

Key point

Choose based on the shape of the problem, not on the popularity of a paradigm. Ask whether the work is mainly a series of operations or whether it is naturally described as interacting entities with their own state and behavior.

Common Design Mistakes

  • Common mistake

    Treating encapsulation as merely putting functions and data next to each other.

    What it looks like

    A design may list related data and functions together but still make the program's main organization revolve around separate data manipulation.

    Why it fails

    Encapsulation is about making the object the unit that combines data with the behavior that operates on it.

    Do this instead

    Ask whether the object represents both the relevant state and the operations associated with that state.

  • Common mistake

    Assuming object-oriented programming is always better.

    What it looks like

    Choosing objects automatically for a small, straightforward sequence of operations.

    Why it fails

    The source material states that procedural programming is often simpler initially and works well for small to medium programs.

    Do this instead

    Compare the problem's structure and scale before selecting a paradigm.

  • Common mistake

    Confusing a class with an object.

    What it looks like

    Treating the class template as though it were one particular value.

    Why it fails

    A class defines structure and behavior, while an object is a specific instance with actual values.

    Do this instead

    Use class for the shared template and object for each concrete instance.

  • Common mistake

    Thinking that data hiding means the object's data has no relationship to outside behavior.

    What it looks like

    Describing the internal data and the object's operations as unrelated parts.

    Why it fails

    The purpose of bundling is to connect the data with the behavior that operates on it.

    Do this instead

    Describe outside use in terms of the object's behavior while recognizing that the behavior operates on the object's data.

Apply the Design Test

EASY

A small program performs a short, straightforward sequence of operations on data. A much larger system contains many entities, each with its own state and behavior. Which design philosophy is a natural starting point for each situation, and what question led you to that choice?

Hints
  • For the first situation, focus on whether the problem naturally decomposes into a sequence of operations.
  • For the second situation, focus on whether the problem naturally decomposes into entities with their own state and behavior.
  • Also consider the difference between small to medium programs and large, complex systems.

What do you think happens?

Before reading the answer, predict which paradigm is usually the simpler starting point for a small, straightforward task.

  • Procedure-oriented programming

  • Object-oriented programming

  • Neither can be appropriate

Reveal answer
Answer:

Procedure-oriented programming

The source material states that procedural code is often simpler to write initially and that procedural programming works well for small to medium programs. The final choice still depends on the problem's structure.

Key Takeaways

  1. 1
    Procedure-oriented programming organizes code around functions that manipulate separate data.
  2. 2
    Object-oriented programming bundles data and the methods that operate on that data into objects.
  3. 3
    Encapsulation connects an object's state with its behavior, while data hiding emphasizes using the object's behavior instead of organizing outside code around its internal data.
  4. 4
    A class is a template defining structure and behavior; an object is a specific instance with actual values.
  5. 5
    Procedural programming often suits small to medium, straightforward programs, while object-oriented programming can scale better for large, complex systems.

At a glance

  • Procedure-oriented design centers on functions that manipulate separate data.
  • Object-oriented design bundles data and behavior into objects.
  • Data hiding creates a conceptual boundary between an object's internal data and the behavior used by outside code.
  • Classes define shared structure and behavior, while objects are individual instances with actual values.
  • The best paradigm depends on whether the problem is mainly a sequence of operations or a collection of entities with their own state and behavior.

Related concepts

More concepts starting with E