Programming Concept

Encapsulation & Objects

Objects, data visibility, accessors/mutators, constructors, composition, design principles.

Chapter 10 (pp. 197–202) of Computer Science I.

Topics

SectionPageContent
Objects198Defining, creating, using objects.
Design Principles & Best Practices200Encapsulation guidance.

Key Ideas

  • Encapsulation — bundle data with behavior; hide internals behind visibility rules.
  • Developed most fully in Java: data visibility, accessor/mutator methods, constructors, composition, equals()/hashCode().
  • PHP has a parallel objects chapter; C approximates with structs + factory functions (no true classes).

Examples

An object bundles data with behavior

object Counter:
  private count ← 0
  method increment():
    count ← count + 1
  method get():
    return count

count is hidden; callers change it only through the methods — that is encapsulation.

In Java

public class Counter {
    private int count = 0;          // hidden state
    public void increment() { count++; }
    public int get() { return count; }
}

private enforces encapsulation; callers reach count only through methods.

Citations

[1] Computer Science I, Ch. 10, pp. 197–202.