Object-oriented programming builds a program from objects that hold their own data and know how to process it. Learn the OOP vocabulary the examiners expect, write classes with private attributes, constructors, getters and setters in 9618 pseudocode, and use inheritance, polymorphism and containment to design a solution.
A class is a blueprint that defines the attributes (properties: the data) and methods (procedures and functions) of a type of object. An object is an instance of a class, created by calling its constructor (NEW). Encapsulation keeps attributes PRIVATE so they can only be read or changed through PUBLIC getters and setters. Inheritance lets a subclass take on the attributes and methods of its superclass and add its own; polymorphism lets a subclass override an inherited method so the same method call behaves differently for different classes. Containment (aggregation) is when an object of one class holds objects of another class as attributes.
In the 9618 pseudocode a class is written between CLASS and ENDCLASS. The constructor is always a procedure called NEW, and the keywords PUBLIC and PRIVATE set who may use each attribute and method.
CLASS Account
PRIVATE AccountID : STRING
PRIVATE Balance : REAL
PUBLIC PROCEDURE NEW(GivenID : STRING)
AccountID ← GivenID
Balance ← 0
ENDPROCEDURE
PUBLIC FUNCTION GetBalance() RETURNS REAL
RETURN Balance
ENDFUNCTION
PUBLIC PROCEDURE Deposit(Amount : REAL)
IF Amount > 0 THEN
Balance ← Balance + Amount
ENDIF
ENDPROCEDURE
PUBLIC FUNCTION Withdraw(Amount : REAL) RETURNS BOOLEAN
IF Amount > 0 AND Amount <= Balance THEN
Balance ← Balance - Amount
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
ENDCLASS
DECLARE MyAccount : Account
MyAccount ← NEW Account("A101")
MyAccount.Deposit(50)
OUTPUT MyAccount.GetBalance()
| Term | Meaning, using the Account class |
|---|---|
| Class | the blueprint: Account defines what every account stores and can do |
| Object / instance | MyAccount is one instance of Account, with its own AccountID and Balance |
| Attribute (property) | a data item of the object: AccountID, Balance |
| Method | a procedure or function in the class: NEW, GetBalance, Deposit, Withdraw |
| Constructor | the method NEW, run when the object is created, which initialises the attributes |
| Getter | a method that returns the value of a private attribute: GetBalance |
| Setter | a method that changes a private attribute, usually after checking the new value: Deposit only accepts a positive amount |
| Encapsulation | attributes and methods are bundled together, and the attributes are PRIVATE so other code cannot set Balance to a nonsense value directly |
OUTPUT MyAccount.Withdraw(80), OUTPUT MyAccount.Withdraw(20), MyAccount.Deposit(-10), OUTPUT MyAccount.GetBalance(). What is output?MyAccount.Balance ← 1000 in the main program would be rejected.PUBLIC FUNCTION GetAccountID() RETURNS STRING / RETURN AccountID / ENDFUNCTION. Balance is declared PRIVATE, so it can only be accessed by the class's own methods; code outside the class must use the public methods.A class and an object are not the same thing. The class is the definition (written once); an object is an instance created from it with NEW, and there can be many objects of one class, each with its own attribute values.
A subclass is declared with INHERITS. It gets every attribute and method of the superclass, adds its own, and can override a method by redefining it with the same name. Its constructor calls the superclass constructor with SUPER.NEW.
CLASS Shape
PRIVATE Name : STRING
PUBLIC PROCEDURE NEW(GivenName : STRING)
Name ← GivenName
ENDPROCEDURE
PUBLIC FUNCTION GetName() RETURNS STRING
RETURN Name
ENDFUNCTION
PUBLIC FUNCTION Area() RETURNS REAL
RETURN 0
ENDFUNCTION
ENDCLASS
CLASS Rectangle INHERITS Shape
PRIVATE Width : REAL
PRIVATE Height : REAL
PUBLIC PROCEDURE NEW(GivenName : STRING, GivenWidth : REAL, GivenHeight : REAL)
SUPER.NEW(GivenName)
Width ← GivenWidth
Height ← GivenHeight
ENDPROCEDURE
PUBLIC FUNCTION Area() RETURNS REAL
RETURN Width * Height
ENDFUNCTION
ENDCLASS
CLASS Circle INHERITS Shape
PRIVATE Radius : REAL
PUBLIC PROCEDURE NEW(GivenName : STRING, GivenRadius : REAL)
SUPER.NEW(GivenName)
Radius ← GivenRadius
ENDPROCEDURE
PUBLIC FUNCTION Area() RETURNS REAL
RETURN 3.142 * Radius * Radius
ENDFUNCTION
ENDCLASS
DECLARE Shapes : ARRAY[1:3] OF Shape
DECLARE Index : INTEGER
Shapes[1] ← NEW Rectangle("R1", 4, 2.5)
Shapes[2] ← NEW Circle("C1", 2)
Shapes[3] ← NEW Rectangle("R2", 3, 3)
FOR Index ← 1 TO 3
OUTPUT Shapes[Index].GetName(), " ", Shapes[Index].Area()
NEXT Index
Shapes[Index].Area() runs a different method depending on the class of the object: Width * Height for a Rectangle, 3.142 * Radius * Radius for a Circle. Each subclass overrides Shape's Area.Inheritance and polymorphism are different marks. Inheritance is the subclass receiving the superclass's attributes and methods; polymorphism is a method with the same name behaving differently in different classes (by overriding).
Four quick checks. Each correct answer earns XP and records this skill on your profile.
What is an object?
What is the name of a constructor in 9618 pseudocode?
Why are attributes usually declared PRIVATE?
The same method call Area() gives different results for a Circle and a Rectangle. What is this?
Not every relationship is inheritance. If an object has other objects as parts, use containment (aggregation): the container holds them as attributes, often in an array.
CLASS Drawing
PRIVATE Shapes : ARRAY[1:20] OF Shape
PRIVATE NumShapes : INTEGER
PUBLIC PROCEDURE NEW()
NumShapes ← 0
ENDPROCEDURE
PUBLIC FUNCTION AddShape(NewShape : Shape) RETURNS BOOLEAN
IF NumShapes < 20 THEN
NumShapes ← NumShapes + 1
Shapes[NumShapes] ← NewShape
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
PUBLIC FUNCTION TotalArea() RETURNS REAL
DECLARE Total : REAL
DECLARE Index : INTEGER
Total ← 0
FOR Index ← 1 TO NumShapes
Total ← Total + Shapes[Index].Area()
NEXT Index
RETURN Total
ENDFUNCTION
ENDCLASS
| Design step | What to look for in the scenario |
|---|---|
| Classes | the main nouns: things the system stores data about (Shape, Drawing, Member, Book) |
| Attributes | the data each thing has; make them PRIVATE and choose a data type for each |
| Methods | what each thing does or what is done to it (verbs); a constructor, getters and setters, plus processing methods |
| Inheritance | "is a": several classes share attributes and methods, so put the shared part in a superclass |
| Containment | "has a": one object is made up of, or holds, objects of another class |
Do not use inheritance for a has-a relationship. A Drawing is not a kind of Shape, so CLASS Drawing INHERITS Shape is wrong; a Drawing contains Shapes.
Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.
Which line correctly begins a subclass Lorry of the class Vehicle?
Inside the constructor of Lorry, how is the constructor of Vehicle called?
A Team object stores an array of Player objects. Which relationship is this?
Which statement correctly creates an object of the class Circle?
This skill is now recorded on your profile. Keep the chain going.