Practice Flashcards
Flip to reveal answersWhat is encapsulation?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All 5 Flashcards — Encapsulation
Sign up free to track progress and get spaced-repetition review schedules.
Question
What is encapsulation?
Answer
Keeping an object's data private and providing public methods as the only way to reach it, so every change goes through code that can check it first rather than any part of the program writing whatever it likes.
Question
What is the difference between encapsulation and information hiding?
Answer
Encapsulation is the mechanism — private attributes, public methods. Information hiding is the principle behind it: the outside should know what a class does, not how, so the inside can be rewritten without breaking callers.
Question
What are getters and setters?
Answer
A getter returns a private value; a setter changes one after checking it. Providing a getter but no setter makes a value readable from outside but not changeable.
Question
Why is a setter with no checks not really encapsulation?
Answer
The data is then effectively public with extra typing. The value of encapsulation lies in the checking that happens on the way in, not in the wrapping itself.
Question
How does a private attribute help when a value is wrong?
Answer
The fault must be inside that class, since nothing outside could have written it. With a public attribute, any line of the whole program could be responsible — the same argument as preferring local variables to global ones.
Read the notes
Full study notes for Encapsulation
Topic 7.1 hub
OOP: a single class
More from Topic 7.1
All flashcards in this topic
Computer Science exam skills
Paper structures & tips
Track your progress with spaced repetition
Sign up free — Aimnova tells you exactly which cards to review and when, so you remember everything before your IB exam.
Start Free