← IB Computer Science

The Four OOP Principles

Topic B3 is HL only (AHL). Object-oriented programming rests on four key principles: encapsulation, inheritance, polymorphism and abstraction. This unit explains each one in the book's terms, and covers the access modifiers (private, protected and public) that make encapsulation work and control what inherited subclasses can see.

Slide 1 / 4

Encapsulation and information hiding

Encapsulation: a capsule that hides its dataclass capsuleprivate data (hidden)- balance- pinkept safe inside+ getBalance()+ deposit()outsidecodevia public methodno direct accesswhy it helps• secure privatefields• use it withoutknowing internals• fix in ONEplace only
  • Encapsulation means data is hidden within a class and accessed only through publicly accessible methods, which promotes code reuse and easier maintenance.
  • The book's metaphor is a capsule: the variables and methods are wrapped inside and kept safe, and you use the class without needing to know how it works internally.
  • Its main principle is information hiding: keeping data protected so it cannot be changed by external sources.
  • Advantages include secure private fields, code that works within its own class, and maintenance changes made in just one place.

Check you've got it

0/4

1. Which statement best describes encapsulation in OOP?

2. Which access modifier allows a variable to be seen by its own class, its subclasses and other classes in the same package, but not by unrelated classes outside the package?

3. A Salesperson class has two overloaded methods: calculateWage() returns annualSalary / 12, and calculateWage(monthlySales) returns annualSalary / 12 plus commission on those sales. An object is created with an annualSalary of 45000 and no commission, then calculateWage() is called with no argument. What is returned?

4. Why can an abstract class not be used to create objects directly?

Free revision content. Multiple-choice answers are marked instantly. Written answers are marked by AI on /free-mark.