Encapsulation and Access Control
The practical payoff of encapsulation is organization. Instead of scattering account-related variables and functions throughout a program, you define them together in a class. The class is written once, and many account objects can be created from it.
From Blueprint to Object
When a program represents something such as a book, bank account, or car, it needs to describe both the information that thing has and the actions it can perform. Object-oriented programming organizes that description with a class. A class is a template or blueprint. An object is a concrete instance created from that blueprint.
A single class can create many objects. The objects share the structure and behavior defined by the class, but each object has its own independent data. For example, a Book class can serve as the blueprint for several book objects, with each object representing a different book.
Fields, Methods, and Attributes
Every class contains two kinds of members: fields and methods. Fields are variables that belong to an object and store its data, also called its state. Methods are functions that belong to a class and define the actions an object can perform, also called its behavior. Together, fields and methods are called the attributes of the class.
| Term | Meaning | Bank account example |
|---|---|---|
| Field | A variable belonging to an object that stores data or state | balance |
| Method | A function belonging to a class that defines behavior | deposit or withdraw |
| Attribute | The collective term for a class's fields and methods | balance, deposit, and withdraw together |
The three terms describe different levels of the same class structure.
Bundling Related Responsibilities
Before object-oriented programming, variables and functions could be written separately. A variable held a piece of data, while a function performed an action. In real systems, however, related data and behavior belong together. A bank account has a balance and operations such as deposit and withdraw. A car has a color and speed and actions such as accelerate and brake.
Encapsulation is the bundling of related data and the functions that work on that data into a single unit called a class. The class gives a related collection of information and behavior one place in the program. This organization makes the code reflect the way the represented thing works: an account has account data and account actions, while a car has car data and car actions.
The practical payoff of encapsulation is organization. Instead of scattering account-related variables and functions throughout a program, you define them together in a class. The class is written once, and many account objects can be created from it.
A Library Book Scenario
Representing Books in a Library
Organize the information and behavior needed to represent books in a library system.
- 1Choose the class
Use Book as the class because it is the reusable blueprint for the book objects the library needs.
- 2Identify the fields
Fields hold the data for each book object. The source scenario establishes the need to represent books; the particular fields should be selected according to the library system's requirements.
- 3Identify the methods
Methods define the behavior that a book object can perform. The relevant actions should be grouped with the book data in the Book class.
- 4Create objects
Create as many book objects as the library needs. Each object follows the Book structure while holding its own independent data.
The Book class keeps the structure and behavior for books in one place, while each book object represents one concrete book with its own data.
This example separates the reusable design from the individual things represented by that design. Book is the class. Each particular book in the library is an object. The fields describe the object's data, and the methods describe what the object can do. All of those members are attributes of the class.
Access Across the Class Boundary
Access control is considered here as the organization of what belongs inside a class and how outside code works with that organized unit. The class boundary groups its fields and methods together, while the object's methods provide the defined behavior associated with the object's data. This lesson focuses on that boundary and bundling idea; the source material does not specify language-specific access levels or rules such as public and private members.
Why the Organization Matters
| Benefit | How classes provide it |
|---|---|
| Clarity | Readers can see an object's data and capabilities in one place. |
| Reusability | A class is defined once and can create as many objects as needed. |
| Maintainability | Changing the class changes the behavior provided to existing and future objects. |
Bundling fields and methods gives object-oriented code practical organizational benefits.
A class is more than a container for names. It provides a reusable place for the data and behavior of a kind of object. A BankAccount class communicates what an account contains and what it can do. If the account's behavior needs to change, the class is the place to make that change, so the organization remains consistent for account objects.
The same pattern appears in ordinary programming values. When you write x = 5, the value 5 is an object that is an instance of the int class. The int class defines what integers are and what operations can be performed on them, while x holds a reference to that object. Many integer objects can be created from the same int class, and each is independent.
Check Your Understanding
Imagine a Car class. Explain which parts of the design would be fields, which would be methods, and what would count as an object. Then explain how putting the car's data and behavior in one class could make the program easier to organize.
Hints
- Fields store an object's data or state.
- Methods define the actions an object can perform.
- An object is one concrete instance created from the Car blueprint.
- Mention clarity, reusability, or maintainability in your explanation.
Common mistake Treating a class and an object as the same thing.
What it looks likeCalling Book and one particular library book interchangeable terms.
Why it failsBook is the blueprint, while the particular library book is an object created from that blueprint.
Do this insteadUse class for the reusable template and object for each concrete instance.
Common mistake Calling only stored data an attribute.
What it looks likeSaying that balance is an attribute but deposit is not.
Why it failsFields and methods together are called the attributes of a class.
Do this insteadRemember that a field is an attribute and a method is also an attribute.
Common mistake Separating related data and behavior unnecessarily.
What it looks likeKeeping account data in one place and account operations scattered elsewhere.
Why it failsThe source identifies bundling related data and functions as a way to improve clarity, reusability, and maintainability.
Do this insteadGroup the fields and methods that belong to the same kind of object in one class.
Common mistake Assuming this overview defines public and private access rules.
What it looks likeApplying a specific language's access-level syntax based only on the word access control.
Why it failsThe provided material explains class boundaries and bundling but does not specify language-specific access levels.
Do this insteadLimit this lesson's conclusion to encapsulating related fields and methods in a class.
Essential Takeaways
- 1A class is a blueprint or template, while an object is a concrete instance created from that class.
- 2Fields store an object's data or state, and methods define the object's behavior.
- 3Fields and methods together are called the attributes of a class.
- 4Encapsulation bundles related data and behavior into one class, improving clarity, reusability, and maintainability.
- 5One class can create many independent objects that share the same structure and behavior.
At a glance
- A class defines a reusable blueprint; each object is a concrete instance with its own data.
- Fields represent stored state, methods represent behavior, and both are attributes.
- Encapsulation keeps related data and functions together in one class.
- This organization supports clearer, more reusable, and more maintainable code.
- The provided concept focuses on class boundaries and bundling, not language-specific public or private access syntax.