Object-oriented programming

A-Level Computer Science · Recursion, OOP and File Handling

Class and object

A class is the template: it defines attributes (the data) and methods (the operations). An object is one instance created from that class, with its own values. A constructor runs when the object is created and sets the initial state.

Encapsulation

Attributes are declared private and reached only through public methods — getters and setters. This matters for two reasons: the class can validate every change (a setter can refuse a negative age), and the internal representation can be changed later without breaking any code that uses the class.

Inheritance

A subclass inherits the attributes and methods of its superclass and adds its own. It models an 'is a' relationship — a SavingsAccount is a BankAccount. The benefit is reuse and a single place to fix shared behaviour; the risk is that a deep hierarchy becomes hard to follow and a change high up affects everything below.

Polymorphism

A subclass can override an inherited method, and the version that runs is decided by the object's actual type. So a loop over a list of Shape objects can call Area() on each without knowing whether it is a Circle or a Rectangle. That is the real payoff of OOP: adding a new shape requires no change to the loop.

Judging OOP fairly

It suits large programs built by teams, because encapsulation limits how far a mistake can spread, and it makes code reusable. For a short script it adds structure that is not needed, and beginners often produce over-complicated hierarchies. Say which context the case describes.

Worked example: designing classes with encapsulation and inheritance

A library system needs to manage books and DVDs. Both have a code, a title, a release year and a borrowed status. Books additionally have an author and a number of pages; DVDs additionally have a running time and an age limit.

Design the classes in pseudocode and explain the three principles used.

Solution

Parent class

CLASS TaiLieu
  PRIVATE Ma : STRING
  PRIVATE TieuDe : STRING
  PRIVATE NamPhatHanh : INTEGER
  PRIVATE DangMuon : BOOLEAN

  PUBLIC PROCEDURE NEW(m, t, n)
    Ma ← m : TieuDe ← t : NamPhatHanh ← n
    DangMuon ← FALSE
  ENDPROCEDURE

  PUBLIC FUNCTION GetTieuDe() RETURNS STRING
    RETURN TieuDe
  ENDFUNCTION

  PUBLIC PROCEDURE ChoMuon()
    IF DangMuon = TRUE THEN
      OUTPUT "Item is already on loan"
    ELSE
      DangMuon ← TRUE
    ENDIF
  ENDPROCEDURE

  PUBLIC FUNCTION MoTa() RETURNS STRING
    RETURN TieuDe & " (" & NamPhatHanh & ")"
  ENDFUNCTION
ENDCLASS

Two child classes

CLASS Sach INHERITS TaiLieu
  PRIVATE TacGia : STRING
  PRIVATE SoTrang : INTEGER
  PUBLIC FUNCTION MoTa() RETURNS STRING  // override
    RETURN TieuDe & " — " & TacGia
  ENDFUNCTION
ENDCLASS

CLASS DVD INHERITS TaiLieu
  PRIVATE ThoiLuong : INTEGER
  PRIVATE GioiHanTuoi : INTEGER
  PUBLIC FUNCTION MoTa() RETURNS STRING  // override
    RETURN TieuDe & " [" & GioiHanTuoi & "+]"
  ENDFUNCTION
ENDCLASS

Where the three principles are used

Encapsulation: every attribute is PRIVATE, read and changed only through public methods. As a result ChoMuon() can check the status before changing it; no outside code can set DangMuon ← TRUE directly, so an item cannot be lent twice.

Inheritance: the shared part (code, title, year, borrowed status) is written once in the class TaiLieu. If the library later adds a new lending rule, only one place needs editing and both kinds of item receive the change.

Polymorphism: both child classes override MoTa(). As a result, the code that prints the catalog can simply loop through an array of TaiLieu and call MoTa(); the right version is chosen automatically by the actual type. Adding a third kind of item (for example a magazine) does not require changing a single line in the catalog-printing code.

Common mistake

Declaring an attribute PUBLIC and still writing getter and setter functions. That is not encapsulation: any code can bypass the setter and modify the attribute directly, so every validation check in the setter becomes meaningless.

Key takeaway

A class is the template, an object is an instance. Encapsulation keeps attributes private to control every change; inheritance models the "is a" relationship and avoids repeated code; polymorphism lets you add new child classes without changing the code that calls them.

← Recursion and binary trees · All A-Level Computer Science lessons · Practice A-Level Computer Science questions (free, instant scoring) · File handling, exceptions and the ethics of computing →