Object-Oriented Programming (OOP) is a programming paradigm that structures programs as collections of objects. Each object combines attributes (data) and methods (behaviours) into a single unit. OOP models real-world entities directly in code.
A class is a blueprint or template that defines the attributes and methods that objects of that type will have. It does not represent a specific entity — it describes the structure.
An object is a specific instance of a class, created from the class blueprint. Each object has its own values for the class's attributes.
Here, Animal is the class (blueprint); cat and dog are two distinct objects (instances), each with their own name and species attribute values.
Encapsulation means bundling an object's data (attributes) and the methods that operate on that data together inside a class, and controlling access to the internal data from outside.
__ in Python, or declared private in Java/C++).External code cannot directly access __balance — it must call getBalance() or deposit(). This protects the balance from invalid direct assignment like account.__balance = -999.
Inheritance allows a subclass (child class) to inherit attributes and methods from a superclass (parent class). The subclass extends the parent, adding or overriding behaviour.
Dog is-an Animal; a SavingsAccount is-a BankAccount.class Dog(Animal):Polymorphism means "many forms" — objects of different classes can be treated through the same interface, but each responds differently to the same method call. It relies on method overriding from inheritance.
makeSound() is defined on Animal, but Dog, Cat, and Bird each override it with their own implementation.The loop does not need to check the type of each object — it calls makeSound() on each, and polymorphism ensures the correct version runs.
Abstraction means hiding complex implementation details and exposing only what is necessary. The user of a class only sees the public interface — the methods and their signatures — not how they are implemented internally.
account.deposit(100), you don't need to know how the balance is stored internally — you just know that calling deposit adds to the balance.A constructor is a special method that runs automatically when an object is created. It initialises the object's attributes.
__init__(self, ...)self parameter in Python refers to the specific instance being created.In the exam you may be given or asked to draw a class diagram. The standard format (UML) shows:
| Section | Contains | Example |
|---|---|---|
| Class name | Name of the class | BankAccount |
| Attributes | Data fields with types and visibility (- private, + public) | - balance : Real - owner : String |
| Methods | Procedure/function names, parameters, return types | + deposit(amount : Real) : void + getBalance() : Real |
Classes can relate to each other in different ways:
Student object attends a Course).Library has Books, but Books exist without the Library).House has Rooms — Rooms don't exist without the House).| Pillar | Meaning | Benefit |
|---|---|---|
| Encapsulation | Bundle data + methods; hide private data | Data protection; reduced coupling |
| Inheritance | Subclass inherits from superclass | Code reuse; models is-a relationships |
| Polymorphism | Same interface, different behaviour | Flexible, extensible code |
| Abstraction | Hide complexity; expose only interface | Simplifies use of complex systems |
8 questions · 20 marks · instantly marked
make and speed, and methods accelerate() and brake(). A subclass Car inherits from Vehicle and adds a numDoors attribute. Draw a UML class diagram for both classes, showing the inheritance relationship.[3 marks]name and grade.[2 marks]| Term | Definition |
|---|