Programming foundations

Stage 5 · Lesson 1 of 10

Classes, objects and encapsulation

Object-oriented design and clean code

A class bundles data with the behaviour that changes it, and an is one instance of that class. The point is control, not syntax. Picture a BankAccount that keeps its balance .

It offers deposit and withdraw methods that refuse a negative amount, so no other code can put it into an invalid state. That idea is encapsulation. Use a class when data and the rules about it belong together. A plain is fine when there is no state (stored data) to protect.

Common trapmaking everything a class because a tutorial did, or exposing every field through a , which gives you the ceremony without the protection.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

  • Request mentoring on a solved exercise and ask specifically about naming and structure

Keep handy

  • Writing tests and assertions sections: the standard way to test Java code

Also worth your time

Mark this stage as not done?