aımnova
DashboardMy LearningCrash CoursesStudy Plan

Aimnova site navigation

Stay in the loop

Get the latest study resources and updates

New features, study tips and exam insights — straight to your inbox.

IB Diploma

  • IB Past Papers
  • IB Study Notes
  • IB Question Bank
  • IB Mock Exams
  • IB Revision

IB Subjects

  • IB Math AA
  • IB Math AI
  • IB Economics
  • IB Business Management
  • IB Physics
  • IB Biology
  • View all IB subjects→

IB Past Papers

  • IB Math AA HL Past Papers
  • IB Math AA SL Past Papers
  • IB Math AI HL Past Papers
  • IB Math AI SL Past Papers
  • IB Economics HL Past Papers
  • IB Economics SL Past Papers
  • IB ESS Past Papers
  • View all past papers→

Study Resources

  • Study Notes
  • Question Bank
  • Mock Exams
  • Crash Courses
  • Flashcards
  • Revision Guide
  • Exam Skills
  • Command Terms
  • Grade Calculator
  • Exam Timetable 2026

Aimnova

  • Features
  • Pricing
  • For Teachers
  • For Schools
  • For Parents
  • About Us
  • Blog
  • Contact
aımnova

IB study, practice and feedback built around the real exam.

TermsPrivacyCookies·© 2026 Aimnova. All rights reserved.c1e448f

Aimnova is not affiliated with or endorsed by the International Baccalaureate Organization (IB).

NotesComputer ScienceTopic 7.1Encapsulation
Back to Computer Science Topics
7.1.55 min read

Encapsulation

IB Computer Science · Unit 7

Smart study tools

Turn reading into results

Move beyond passive notes. Answer exam-style questions, get AI feedback, and build the skills that earn top marks.

Get Started Free

Contents

  1. 1Only one way in
  2. 2Private, public, and getters
  3. 3Why it matters
  4. 4Exam-style question
The big idea: Encapsulation keeps an object's data private and provides public methods as the only way to reach it.

So every change goes through code that can check it first — instead of any part of the program writing whatever it likes.

Step to 'Encapsulation': direct assignment refused, the public method allowed.

Interactive diagram

Explore the labelled diagram, charts and maps for this topic in full study mode.

Claim your free topic

Free preview

This is the free notes preview

You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:

  • FlashcardsLock in vocabulary and key terms with spaced repetition.
  • Practice questionsAnswer exam-style questions and get instant AI marking.
  • Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
  • Personalised study planA daily plan built around your exam date and weak areas.
Start Studying Free Full access to Aimnova Pro · cancel anytime
class Account:
    def __init__(self, owner):
        self.owner    = owner
        self.__balance = 0          # private (Java: private double balance)

    def deposit(self, amount):      # the controlled way in
        if amount <= 0:
            return False            # refuse nonsense before it is stored
        self.__balance = self.__balance + amount
        return True

    def get_balance(self):          # a getter: read, but not write
        return self.__balance

a = Account("Ana")
a.deposit(50)
print(a.get_balance())              # 50
a.__balance = 1000000               # does not reach the real attribute
class Account {
    String owner;
    private double balance = 0;      // private — unreachable from outside

    Account(String owner) {
        this.owner = owner;
    }

    boolean deposit(double amount) {  // the controlled way in
        if (amount <= 0) {
            return false;             // refuse nonsense before it is stored
        }
        this.balance = this.balance + amount;
        return true;
    }

    double getBalance() {             // a getter: read, but not write
        return this.balance;
    }
}

Account a = new Account("Ana");
a.deposit(50);
System.out.println(a.getBalance());   // 50.0
a.balance = 1000000;                  // will not compile — balance is private

Private

  • Reachable only from inside the class
  • Python marks it with two underscores; Java writes private
  • The normal choice for attributes

Public

  • Reachable from anywhere
  • The normal choice for methods
  • This is the class's agreed way of being used

Getters and setters

  • A getter returns a private value: getbalance()
  • A setter changes one after checking
  • A getter with no setter makes a value read-only from outside
Information hiding is the wider idea: Encapsulation is the mechanism — private data, public methods.

Information hiding is the principle: the outside should know what a class does, not how. So the inside can be rewritten without breaking anything that uses it.

Get feedback like a real examiner

Submit your answers and get instant feedback — what you did well, what's missing, and exactly what to write to score full marks.

Try AI Tutor FreeYour first topic is free to keep • No credit card required
1

Rules are enforced at the door

deposit() refuses a negative amount, so a balance can never be reduced by a deposit.

With a public attribute, every piece of code touching it would have to remember that rule — and one of them would not.

2

The object is always valid

If every change goes through checked methods, an object cannot reach an impossible state.

A book cannot be on loan twice; an account cannot hold an amount nobody deposited.

3

The inside can change

Store the balance in pence instead of pounds, and only the class changes.

Every caller uses getbalance() and never knew how it was stored — which is information hiding paying for itself.

4

The cause of a bug is nearby

If a balance is wrong and it is private, the fault is in this class.

If it were public, any line of the whole program could have written it — the same argument as local versus global variables.

A getter and setter for everything is not encapsulation: If every private attribute has a setter that changes it with no checks, the data is effectively public with extra typing.

The value is in the checking — and in not providing a setter at all for things the outside has no business changing.

How this is tested — you must say what the private attribute prevents, and what the public method checks. It comes up two ways:

Paper 2 — working with code

  • Explain encapsulation and information hiding, 3-5 marks
  • Apply private and public to a given class
  • Say why an attribute should not be public

Paper 2 — the algorithmic-thinking question

  • Reason about access with no code shown
  • Explain what could go wrong without it
The classic trap: Defining encapsulation without saying what it prevents. The mark is for the consequence: a balance set to a negative number, a book lent twice — something the checks refuse.
IB-style questionExplain[5 marks]

A Thermostat class holds a target temperature that must stay between 5 and 30 degrees. Explain how encapsulation would enforce this, and what could go wrong without it.

Model answer plan

See the answer broken into clear points, with guidance on how each point earns marks.

Claim your free topic

Try an IB Exam Question — Free AI Feedback

Test yourself on Encapsulation. Write your answer and get instant AI feedback — just like a real IB examiner.

what accessor and mutator methods are. [2 marks]

Related Computer Science Topics

Continue learning with these related topics from the same unit:

7.1.1Fundamentals of OOP7.1.2Designing classes7.1.3Static vs non-static7.1.4Classes and objects
View all Computer Science topics
Improve your exam techniqueCommand terms, paper structure, and mark-scheme tips for Computer Science
Previous7.1.4Classes and objects

49 exam-style questions ready for you

Students who practice on Aimnova improve their scores by 15% on average. Get instant feedback that shows exactly how to improve your answers.

Practice Now — FreeView All Computer Science Topics