1 / 8
Programming · Grade 11 · Chapter 6

Python: OOP Deep Dive

Inheritance and encapsulation — building on what we know.

Here's a problem

Repeating the Same Class Twice

A "Teacher" class and a "Student" class both need name, age, and email — but a Teacher also needs a subject, and a Student needs a grade level. Writing name/age/email twice feels wasteful.

Discuss before revealing: What if both classes could "inherit" the shared parts from one common base class?
Today's tool

Inheritance and Encapsulation

Inheritance lets a class reuse attributes/methods from a parent class, avoiding duplication. Encapsulation means keeping an object's internal data protected, only exposing what's needed.

How it looks

A Base Class and a Child Class

# base class
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

# inherits from Person
class Student(Person):
  def __init__(self, name, age, grade):
    super().__init__(name, age)
    self.grade = grade
Let's build it together

Extend a Base Class

We'll extend a Person base class into a Student and a Teacher class, sharing common attributes.

Activity: Live-code a Person base class, then create Student and Teacher subclasses that inherit from it and add their own specific attributes.
Quick check

What does inheritance let a class do?

AReuse attributes and methods from a parent class
BDelete other classes
BRun faster automatically
DAvoid using functions
Click to reveal answer
Let's discuss

Where might inheritance help in your capstone project?

Thinking ahead to your Innovation Challenge project — what related objects might share common structure through inheritance?

Before you go

Today we learned...

Inheritance avoids duplicated code across related classes. Next week: connecting to AI through real APIs!