Learning Objectives
By the end of this topic you will be able to:
Define class, object, attribute and method
Explain encapsulation, inheritance, polymorphism and abstraction
Describe constructors and instantiation
Understand class diagrams and write/trace OOP code
Inheritance
Inheritance
Inheritance allows a subclass (derived class) to inherit all the attributes and methods of a superclass (base class), and add or override additional behaviour. Promotes code reuse.
Example: Animal is a superclass with attribute name and method speak(). Dog inherits from Animal and adds method fetch(). Dog does not need to redefine name.
Single inheritance: one parent class. Multiple inheritance: inherits from multiple parents (supported in Python, not Java).
Constructor chaining: a subclass constructor typically calls the parent class constructor (using super()) to initialise inherited attributes before adding its own.
Polymorphism & Abstraction
Polymorphism and Abstraction
Polymorphism
Polymorphism means “many forms” — the same method name behaves differently depending on the object it is called on. A speak() method in Dog returns “Woof” while in Cat it returns “Meow”. Method overriding achieves this.
Abstraction
Abstraction hides the complex implementation details from the user and exposes only the necessary interface. Abstract classes define method signatures that subclasses must implement, without providing the implementation themselves.
Constructor: a special method called when an object is instantiated. Initialises the object’s attributes. In Python: def __init__(self, name): self.name = name
Common Mistakes
Don’t Lose Marks
!
Confusing a class with an object — a class is the blueprint/template (the definition). An object is a specific instance created from the class. A class can produce many objects, each with their own attribute values.
!
Saying encapsulation just means private variables — it means bundling data and methods together AND restricting access. The combination of both is the key concept, not just one element.
!
Confusing inheritance and polymorphism: inheritance is about reusing code from a parent class; polymorphism is about the same method name having different behaviours in different subclasses. They work together but are distinct concepts.