Object-Oriented Programming
Object-oriented programming (OOP) models a program as a set of interacting objects, each an instance of a class that bundles together its own data (attributes) and the procedures/functions (methods) that act on that data. Encapsulation restricts an object's attributes to PRIVATE, only reachable from outside the class through its PUBLIC methods, protecting the object's internal state from being changed in invalid ways. Inheritance lets a subclass automatically gain the attributes and methods of a superclass, then add new ones or override an inherited method with its own version. Polymorphism means that calling the same method name on objects of different classes in an inheritance hierarchy runs each object's own (possibly overridden) version, which is what lets code written against a superclass work correctly with any of its subclasses.
Before you start
Make sure you're comfortable with these topics first:
Method
- Identify each class the problem needs as a real-world 'thing' (a noun), listing its attributes (the data it needs to store) and its methods (the actions it can perform) before writing any code.
- Write a constructor (NEW) for every class that sets each attribute to a starting value from the parameters passed in when an object is created.
- Mark every attribute PRIVATE by default, then write PUBLIC accessor (getter) and mutator (setter) methods for any attribute that code outside the class genuinely needs to read or change, rather than exposing the attribute directly.
- When two classes share attributes and methods but one is a more specific version of the other, use INHERITS to make the specific class a subclass of the general superclass, rather than duplicating the shared code.
- Override an inherited method in a subclass whenever that subclass needs different behaviour for the same method name, and call SUPER.NEW(...) inside a subclass's constructor to run the superclass's own constructor first.
- When tracing polymorphic code, always check the object's actual (most specific) class first, since a method call resolves to that class's own version of the method if it has overridden it, even if the call happens inside code written in a superclass.
- Check the finished design against the two hallmark benefits of OOP: could a new subclass be added without changing existing code, and is each object's internal data safe from being corrupted by code outside the class.
Worked example
Two classes are defined as follows: CLASS Shape PRIVATE name : STRING PUBLIC PROCEDURE NEW(n : STRING) name <- n ENDPROCEDURE PUBLIC FUNCTION Area() RETURNS REAL RETURN 0 ENDFUNCTION PUBLIC PROCEDURE Describe() OUTPUT name, " has area ", Area() ENDPROCEDURE ENDCLASS CLASS Square INHERITS Shape PRIVATE side : REAL PUBLIC PROCEDURE NEW(s : REAL) SUPER.NEW("Square") side <- s ENDPROCEDURE PUBLIC FUNCTION Area() RETURNS REAL RETURN side * side ENDFUNCTION ENDCLASS The following code then runs: mySquare <- NEW Square(4) mySquare.Describe() Trace this code and state exactly what it outputs.
- NEW Square(4) calls Square's constructor with s = 4.
- Square's constructor calls SUPER.NEW("Square"), running Shape's constructor and setting name <- "Square".
- Square's constructor then sets side <- s, so side = 4; mySquare is now a Square object with name = "Square" and side = 4.
- mySquare.Describe() is called; since Square does not override Describe, Shape's version runs: OUTPUT name, " has area ", Area().
- Because Area() is called polymorphically on mySquare, and mySquare is actually a Square object, Square's overridden Area() runs instead of Shape's, returning side * side = 4 * 4 = 16.
- Final answer: the program outputs: Square has area 16.
Practice questions
Try each question, then tap to reveal the answer.
Q1State the difference between a class and an object.Show answer
Answer: A class is the template/blueprint that defines what attributes and methods its objects will have; an object is one specific instance created from that class, with its own actual attribute values.
Q2State the purpose of a constructor method in a class.Show answer
Answer: To initialise a new object's attributes with starting values (often passed in as parameters) at the moment the object is created.
Q3Explain why an attribute is normally declared PRIVATE rather than PUBLIC in a well-encapsulated class.Show answer
Answer: So that code outside the class cannot read or change the attribute directly; instead it must go through the class's own PUBLIC methods, which can validate any new value before it is accepted, protecting the object from being put into an invalid state.
Q4A class Employee has a subclass Manager, declared using INHERITS. State two things the Manager class automatically gains from Employee without needing to redefine them.Show answer
Answer: All of Employee's (accessible) attributes, and all of Employee's methods; for example, an attribute like name or salary and a method like GetPay(), unless Manager chooses to override them.
Q5Define what it means for a subclass to 'override' a method.Show answer
Answer: The subclass defines its own version of a method that already exists (with the same name) in its superclass, so that objects of the subclass run the subclass's version of the method instead of the superclass's version.
Q6State what the keyword SUPER is used for inside a subclass's constructor.Show answer
Answer: To call the superclass's own constructor (or another superclass method) directly, so the superclass's part of the object can be initialised before the subclass adds its own attributes.
Q7Explain what is meant by 'polymorphism' in object-oriented programming.Show answer
Answer: The ability for objects of different classes, usually related by inheritance, to respond to the same method call each in their own way; the same method name run on different objects can produce different, class-appropriate behaviour.
Q8A UML class diagram shows an attribute preceded by a minus sign (-) and a method preceded by a plus sign (+). State what each symbol represents.Show answer
Answer: The minus sign (-) represents PRIVATE access, and the plus sign (+) represents PUBLIC access.
Exam-style questions
Written in the style of a A Level Computer Science exam paper, with a full mark scheme.
State two features that encapsulation provides to a well-designed class.
Show mark scheme
Tick each line you got. Your score builds from the marks on the scheme.
Nothing ticked yet - 2 available
A class is defined as follows: CLASS BankAccount PRIVATE balance : REAL PUBLIC PROCEDURE NEW(startingBalance : REAL) balance <- startingBalance ENDPROCEDURE PUBLIC PROCEDURE Withdraw(amount : REAL) IF amount <= balance THEN balance <- balance - amount ELSE OUTPUT "Insufficient funds" ENDIF ENDPROCEDURE PUBLIC FUNCTION GetBalance() RETURNS REAL RETURN balance ENDFUNCTION ENDCLASS The following code then runs: acc <- NEW BankAccount(100) acc.Withdraw(30) acc.Withdraw(90) OUTPUT acc.GetBalance() Trace this code and state everything the program outputs, in order. Explain why balance cannot be read or changed directly from outside the class (e.g. by writing acc.balance).
Show mark scheme
Tick each line you got. Your score builds from the marks on the scheme.
Nothing ticked yet - 5 available
A developer is designing classes for a company's payroll system. She has classes for FullTimeEmployee and PartTimeEmployee, which currently duplicate the name, employeeID and constructor logic in both classes separately. Explain how inheritance could be used to remove this duplication, and evaluate one advantage and one disadvantage of doing so.
Show mark scheme
Tick each line you got. Your score builds from the marks on the scheme.
Nothing ticked yet - 6 available
See real A Level Computer Science past-paper questions, with official mark schemes →
Free printable worksheet
Want more practice on paper? Download the object-oriented programming worksheet pack - 19 pages of exam-style questions with a full mark scheme. One email opens every download in this browser for 14 days - no account, no card. Print it for personal and classroom use.
Next topics
Not quite what you needed?
Tell us what is missing on object-oriented programming, or which topic to write up next. Every request is read, and we reply to every one.
Build a full practice pack.
This topic is one of hundreds in the library - pick the ones a student needs and generate a printable PDF in minutes.