Encapsulation (OOP)

Shared by a Flashcards.gg user · generated with AI · Report this set

This set covers encapsulation in object-oriented programming, including data hiding, access modifiers, and internal state protection.

50 cards English Level: University Programming Published
Tap the card to flip it

Cards · 50

Encapsulation definition
Bundling data and the methods that operate on that data into a single unit while restricting direct access to some components.
Primary purpose of encapsulation
To prevent unauthorized modification, protect internal state integrity, and hide implementation complexity.
Two main aspects of encapsulation
Data bundling and data hiding.
Data bundling
Packaging attributes and corresponding functions together inside a unified entity called an object or class.
Data hiding
Restricting direct external access to an object's internal fields to protect state consistency.
Access modifiers
Keywords in programming languages that declare the scope and accessibility of classes, methods, and variables.
Private access modifier
Restricts visibility so the member can only be accessed within the declaring class itself.
Public access modifier
Allows unrestricted access to the member from any other class or module.
Protected access modifier
Allows access within the declaring class, its subclasses, and sometimes the same package.
Default or package-private access
Restricts access strictly to classes within the same package or namespace when no modifier is specified.
Getter method
A public method providing controlled, read-only access to a private attribute.
Setter method
A public method that updates a private attribute, often validating input data before assignment.
Data validation via setters
The practice of rejecting invalid, out-of-range, or malformed data before modifying an internal variable.
Information hiding vs. encapsulation
Information hiding is a theoretical design principle, while encapsulation is the concrete language mechanism used to enforce it.
Abstraction vs. encapsulation
Abstraction focuses on showing what an object does, whereas encapsulation focuses on hiding how it does it and protecting internal data.
Internal state
The collection of current values held by an object's private variables.
Class invariant
A condition or rule that must always hold true for an object throughout its entire lifecycle.
How encapsulation protects invariants
By forcing state changes to go through methods that verify rules before committing changes.
Read-only property
An attribute exposed only with a getter and no setter, preventing external modification after instantiation.
Write-only property
An attribute that has a setter but no getter, useful for sensitive data like passwords.
Immutable object
An object whose state cannot be modified at all after it is constructed.
Encapsulation and immutability
Encapsulation enables immutability by making fields private and final, omitting setters, and returning defensive copies.
Defensive copying
Returning or storing a duplicate of a mutable object to prevent outside code from mutating the internal reference.
Encapsulation in Java
Implemented using private fields with public getters, setters, and explicit access modifier keywords.
Encapsulation in C++
Implemented using class visibility sections labeled private, protected, and public.
Encapsulation in Python
Convention-based, using a single leading underscore for internal members and double underscores for name mangling.
Python property decorator
A built-in feature providing getter, setter, and deleter syntax over encapsulated attributes without breaking API access.
Name mangling in Python
The interpreter renames double-underscore attributes to avoid naming collisions in subclasses.
Encapsulation in modern JavaScript
Enforced at the language level using a hash symbol prefix to declare truly private class fields.
JavaScript closures for encapsulation
Using function scope to keep variables private and inaccessible from outside the closure.
Black-box concept
Viewing a system purely in terms of its external inputs and outputs without knowing its internal implementation.
Public interface
The set of public methods, properties, and constants through which external code interacts with an object.
Implementation details
The internal algorithms, data structures, and private fields invisible to outside consumers.
Coupling
The degree of direct interdependence between software modules.
How encapsulation reduces coupling
It prevents external components from relying on internal structure, allowing modules to change independently.
Cohesion
The degree to which elements inside a single module or class belong together and support a single purpose.
How encapsulation enhances maintainability
Developers can safely refactor internal logic without altering the public API or breaking consuming code.
Side effects in programming
Unexpected modifications to state outside of a function's local scope.
How encapsulation minimizes side effects
It ensures state transitions only happen through explicit, controlled entry points.
Law of Demeter
A design guideline stating that an object should talk only to its immediate collaborators, reinforcing encapsulation.
Tell, Don't Ask principle
Instructing an object to perform an action rather than querying its state and making decisions on its behalf.
Anemic Domain Model
An anti-pattern where objects contain only data and no business logic, violating object encapsulation.
Over-encapsulation
Excessively wrapping trivial data in layers of boilerplate getters and setters without providing real behavioral abstraction.
Module-level encapsulation
Hiding internal functions and data structures within a file, library, or module, exposing only an export list.
Encapsulation in networking
Wrapping protocol data units with headers and footers as data descends through protocol layers.
Encapsulation vs. inheritance
Inheritance can weaken encapsulation by exposing parent internals to subclasses via protected visibility.
Composition over inheritance
A design principle favoring object composition because it preserves encapsulation better than subclassing.
Leaky abstraction
A flaw where internal implementation details unintentionally manifest through the public interface.
Refactoring benefit of encapsulation
Enables changing an underlying data structure, such as an array to a hash map, without affecting calling code.
Encapsulation summary
Shielding internal component complexity and state behind a stable, well-defined public interface.