The core of Object-Oriented Programming — this is arguably the single most important subtopic in the whole syllabus for the practical exam and PAT.
A class is a blueprint defining the fields (data/attributes) and methods (behaviour) that its objects will have.
| Concept | Description |
|---|---|
| Field / method | A variable (field) or function (method) that belongs to a class |
| Static field/method | Belongs to the class itself, shared across all objects (also called a class field/method) — accessed without needing an instance |
| Non-static field/method | Belongs to a specific object (instance) — each object has its own copy of the value |
| Instance | A specific object created from a class |
| Instantiation | The act of creating an object from a class using the `new` keyword |
Access modifiers control visibility:
| Modifier | Meaning |
|---|---|
| public | Accessible from anywhere, including outside the class |
| private | Accessible only from within the same class — the basis of encapsulation |
| protected | Accessible within the same class and by subclasses (see inheritance, 12.4.5) |
Constant fields (final in Java) are declared once and can never be changed afterwards.
Constructors:
Example
public class Learner { private String name; private int grade; // Parameterised constructor public Learner(String name, int grade) { this.name = name; this.grade = grade; } // Accessor (getter) public String getName() { return name; } // Mutator (setter) public void setGrade(int grade) { this.grade = grade; } // toString method public String toString() { return name + " (Grade " + grade + ")"; } }
Method overloading — defining multiple methods with the same name but different parameter lists (different number or types of parameters) within the same class.
Example
public double calculateArea(double side) { return side * side; } // for a square public double calculateArea(double length, double width) { return length * width; } // for a rectangle
Dynamic binding — the process by which the correct overloaded (or overridden — see 12.4.5) method is determined and called at runtime, based on the arguments provided or the actual object type.
Private helper methods — methods marked private that exist only to support other methods within the same class, not intended to be called from outside.
Parameter passing and return types — reusing the concepts from 10.4.6, now within the context of object methods, e.g. passing values into a constructor, or a method returning a computed field value.
Class Diagrams (UML-style) visually represent a class's fields and methods along with their access modifiers, typically using +/− symbols for public/private.
Example
Class Diagram notation: +---------------------+ | Learner | +---------------------+ | - name: String | | - grade: int | +---------------------+ | + getName(): String | | + setGrade(int): void| +---------------------+
The same class, rendered as a UML class diagram — minus (-) is private, plus (+) is public.
A class conceptually represents the backend — its logic and data exist independently of whatever user interface/frontend is used to interact with it, which is why the same class can be reused across a console app, a GUI, or even a different program entirely.
💡 Exam Tip
Encapsulation is examined constantly: expect to be asked WHY fields should be private with public accessors/mutators rather than simply public — the answer is control (validation in setters) and protection from unintended external changes.