Week 4First TermMonday 28 September 2026 to Friday 2 October 2026

Object-Oriented Programming

Classes, Objects, Attributes and Methods; Encapsulation and Inheritance; Polymorphism and Comparing OOP with Procedural Programming; Advanced Data Structures: Arrays and Records; Non-Linear Data Structures: Trees and Graphs; File Type Classifications

Print lesson pack
On this page9 sections
  1. 01Learning Objectives
  2. 02Classes, Objects, Attributes and Methods
  3. 03Encapsulation and Inheritance
  4. 04Polymorphism and Comparing OOP with Procedural Programming
  5. 05Advanced Data Structures: Arrays and Records
  6. 06Non-Linear Data Structures: Trees and Graphs
  7. 07File Type Classifications
  8. 08Practical Work
  9. 09Scenario-Based Questions

Learning Objectives

  • Describe classes, objects, attributes and methods.
  • Explain encapsulation and inheritance.
  • Explain polymorphism and compare OOP with procedural programming.
  • Describe arrays, records, trees and graphs.

Classes, Objects, Attributes and Methods

Object-oriented programming (OOP) organises code into classes and objects. A class is a blueprint or template that describes what data and behaviour every object of that type will have. It defines the attributes, the data each object holds, and the methods, the behaviour each object can perform. The class does not hold specific data itself; it is the drawing of the house, while each object is an actual house built from that drawing. In JavaScript a class is defined with the class keyword and a constructor that runs when an object is created, setting the initial values of the attributes with the keyword this. A Student class, for example, has a constructor receiving name, admissionNo and score, and two methods, isPassing, which returns true when the score is 50 or above, and display, which prints the student's name and score.

An object is a specific instance of a class, created with the new keyword. Each object carries its own copy of the attributes, so student1.name is Chidinma Okoro while student2.name is Emeka Nwosu, even though both come from the same class blueprint. This is how one class models many real things of the same type, all the students of a school, all the accounts of a bank or all the books of a library. Practical work for this part is to design a BankAccount class with attributes for holder, account number and balance, and methods to deposit, withdraw and display the balance, then to create two accounts and perform several operations on each. The same class is used twice, but the balances remain separate.

Encapsulation and Inheritance

Encapsulation is the principle of bundling data and methods inside a class and restricting direct access to some attributes from outside the class. This protects the data from being changed in unexpected ways. In a bank app the balance must not be changed directly from outside the account object; it should only be changed through deposit and withdraw methods, which check that the values are valid. In JavaScript a private field is declared with a hash symbol, such as #balance, and can only be changed from inside the class through controlled methods. A deposit method adds only when the amount is positive; a withdraw method subtracts only when sufficient funds exist and otherwise reports insufficient funds. Encapsulation prevents accidental or malicious changes, makes debugging easier because every change flows through defined methods, and allows the internal design to evolve without breaking external code. In a school system grades should only be changed through proper processes, and encapsulation enforces that discipline in code.

Inheritance is a mechanism where a new class, called the child or derived class, inherits all the attributes and methods of an existing class, called the parent or base class. The child can use everything from the parent and can also add new features or change existing ones. This promotes code reuse, because the child does not rewrite what the parent already provides. A Person parent class might hold name and age and provide a display method. A Student child class extends Person with the super call to invoke the parent constructor, adds admissionNo and score, and extends the display method to show the extra data. The super call is how the child connects to its parent, both for construction and for reusing a parent method.

Polymorphism and Comparing OOP with Procedural Programming

Polymorphism means many forms. In OOP it means that different classes can implement the same method name in different ways, and the correct version runs automatically according to the object's type. A Circle class and a Rectangle class can both have an area method, but Circle returns pi times radius squared while Rectangle returns length times width. A loop that calls area on a list of mixed shapes does not need to know which shape it holds, because each object answers in its own way. In a school context a Teacher, a Cleaner and a SecurityOfficer each have a work method with different behaviour, and one loop can call work on all three. Adding a new staff type later does not change the loop, which is exactly the flexibility real projects need.

Procedural programming organises a program as functions that act on data stored separately, so data must be passed explicitly between functions and any part of the program can reach it. Object-oriented programming bundles related data and behaviour together in classes, so objects carry their own data and protect it through encapsulation. For large projects, OOP is easier to maintain because related code is grouped; for small simple programs, procedures remain perfectly appropriate. The two approaches are not enemies. Many real systems use procedures for simple logic inside objects.

A complete OOP example brings the ideas together. An Animal class holds a name and a sound method returning some sound. A Dog child class overrides sound to return Woof, and a Cat child class overrides it to return Meow. A list holding a Dog and a Cat, processed by one loop that calls sound, demonstrates inheritance, because both children reuse the parent name attribute, and polymorphism, because each answers the same method name in its own way. Scenario question for the systems work, a school library system needs a Book class holding title, author and ISBN, a Member parent class with borrowBook and returnBook, and StudentMember and TeacherMember children with different borrowing limits, naturally modelled with inheritance, encapsulation and polymorphism.

Advanced Data Structures: Arrays and Records

A single value, such as the name of one learner, is stored in a simple variable, but real programs handle many values at once, such as the names of all learners in a class. Data structures are the ways a program arranges collections of values so they can be stored, searched and processed efficiently. An array holds many values of the same type under one name with numbered positions, such as a list of term scores. A record groups different fields about a single item, such as name, admission number, class and score for one learner.

A simple variable holds one value; a data structure holds many related values.

An array stores many values of the same type with numbered positions.

Arrays suit lists such as class scores, phone contacts or product prices.

A record groups different fields about one item, such as name, score and class.

Programmers often combine the two: an array of records holds the whole class, and each record holds one learner details. This leads naturally into classes and objects, where each object groups its own data and behaviour.

Non-Linear Data Structures: Trees and Graphs

Arrays and records arrange data in a straight order, but many relationships branch or connect in many directions. A tree is a data structure with a single root whose branches spread downward into children and leaves with no loops. The folders in a computer form a tree, and a school directory of departments, classes and learners is also a tree. A graph is more general: nodes connected by edges, possibly with loops, like a road network or the connections between computers on the internet.

Tree: one root whose branches spread into children and leaves.

Graph: nodes connected by edges, possibly with loops, like roads or the internet.

File folders and school directories behave like trees.

The internet, road maps and friendships on social media behave like graphs.

A phone network uses graphs to route calls between cities, a bank uses a tree to organise branches, and navigation apps use graphs of streets to find the quickest route. Different problems call for different structures.

File Type Classifications

Every file belongs to a type that tells the computer what it contains and which program should open it. The most common way to identify a file type is by its extension, the letters after the final dot in the file name. Types are also grouped by content: text files hold characters, image files hold pictures, audio files hold sound and video files hold moving pictures.

Text files (.txt,.docx) hold letters, words and documents.

Image files (.jpg,.png) hold photographs and graphics.

Audio files (.mp3,.wav) hold music, speech and sound.

Video files (.mp4) hold moving pictures.

Data files (.xlsx,.csv) hold organised tables of values.

In a school office, the secretary keeps lesson notes as Word files, exam results as spreadsheets, the school logo as an image and assembly music as an audio file. The extension tells the computer which application to open, so knowing file classifications helps learners manage their work and supports the data organisation skills needed for the OOP capstone project.

FeatureProcedural programmingObject-oriented programming
OrganisationFunctions act on data kept separatelyClasses bundle data and methods together
Data handlingData passed between functionsData held as attributes inside objects
ReusabilityFunctions reusable, data passed explicitlyObjects carry their own data and behaviour
Large projectsHarder to maintain as code growsEasier to maintain with related code grouped
Real-world modellingLess natural for entitiesNatural for students, accounts, vehicles
SecurityData reachable from anywhereEncapsulation restricts direct access

Practical Work

  1. Write a class with attributes and a method.
  2. Explain how a subclass inherits from a parent class.
  3. Give one example each of a tree and a graph.
  4. Compare OOP and procedural programming in a table.

Scenario-Based Questions

  1. A school management system has classes Student, Teacher and Course. Student has attributes name and scores, and a method average(). Teacher has attributes name and department. Course has attributes title and a list of Student objects. Design these three classes by writing their attribute lists and method names. Explain how encapsulation protects student scores from being changed by unrelated code.
  2. A bank has a base class Account with attributes owner and balance and methods deposit and withdraw. It creates two child classes SavingsAccount (which adds an interest rate and a method applyInterest) and CurrentAccount (which adds an overdraft limit). Write the class hierarchy and explain how inheritance avoids rewriting deposit and withdraw.
  3. A school project simulates different vehicles. A base class Vehicle has a method move(). A Car child class overrides move to print 'Driving on road', a Boat child class overrides move to print 'Sailing on water' and a Plane child class overrides move to print 'Flying in air'. Explain how polymorphism allows one function driveAll that takes any Vehicle object and calls move() without knowing the exact type.
  4. Compare procedural and object-oriented approaches for building a school grading system. List two advantages of OOP over procedural programming when the system must later add new features such as attendance tracking and fee management.
  5. A programmer creates two Student objects: student1 = Student('Ada', 88) and student2 = Student('Ada', 88). Explain whether these are the same object in memory. Then describe what the constructor (__init__) does and why it is essential for setting initial attribute values.
  6. An online game has a base class Character with health and attack power. A Warrior child class adds a shield attribute, and a Mage child class adds a spellPower attribute. Both override an attack() method differently. Explain how inheritance and polymorphism together allow the game to process a list of characters of mixed types in a single loop.
  7. A programmer has an existing Animal class with a sound() method returning 'Some sound'. They create Dog, Cat and Cow child classes, each overriding sound(). Write the three child class definitions and explain why adding a new Animal type later requires no changes to code that already processes Animal objects.