1/30
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
what is inheritance in object-oriented programming?
a mechanism where a class (child/subclass) automatically receives attributes and methods from another class (parent/base class)
why should you use inheritance?
to avoid duplicated code, share common functionality, and make code easier to maintain
what is a base class?
the class being inherited from
Subclass = child class
Superclass = parent class
Subclass inherits:
✔ attributes
✔ methods
✔ behaviorwhat is a subclass?
a class that inherits from a base class
“What does isinstance(obj, Class) check?”
whether obj is an instance of Class or any of its parent classes
returns True if it is
returns False if not
class Student(Client):
def __init__(self, name, email, major):
super().__init__(name, email) # call Client constructor
self.major = major
def enroll(self):
return f"{self.name} enrolled in classes."is a student also a client?
Yes, because Student inherits from Client. a student:
has everything a client has
PLUS student-specific things (like a major, enrolling, etc.)
why is isinstance() useful?
to prevent bugs by ensuring the correct object type is passed
what does super() do?
gives access to the parent class’s methods
why use super() in a constructor?
to reuse initialization code from the parent class
How do you call a parent class constructor in a subclass?
Use super().__init__(arg1, arg2, ...) with the same arguments the parent expects.
if A inherits from B, and B inherits from C, what does A inherit?
everything from B and C

if Firstyear inherits from Student, which inherits from Client, what methods does Firstyear have?
all methods from Firstyear, Student, and Client
what is the “is-a” rule?
a subclass should represent a more specific version of the parent class
when should you use introduce inheritance?
when you notice duplicated code that can be rafctored into a shared parent class

Why did we create an Expense parent class when refactoring Trip and EquipmentOrder?
Because both classes shared the same attributes and logic (cost, reimburse()), so extracting that shared behavior into a parent class reduces duplication and makes the design cleaner.
class Trip:
def __init__(self, cost, start_date, end_date):
self.cost = cost
self.start_date = start_date
self.end_date = end_date
def reimburse(self):
return self.cost * 0.8
class EquipmentOrder:
def __init__(self, cost, item):
self.cost = cost
self.item = item
def reimburse(self):
return self.cost * 0.8
what shared functionality did Trip and EquipmentOrder have?
cost, reimbursed, and reimburse()
class Trip:
def __init__(self, cost, start_date, end_date):
self.cost = cost
self.start_date = start_date
self.end_date = end_date
def reimburse(self):
return self.cost * 0.8
class EquipmentOrder:
def __init__(self, cost, item):
self.cost = cost
self.item = item
def reimburse(self):
return self.cost * 0.8
how was this refactored?
A new parent class Expense was created to hold shared attributes and methods (cost, reimburse()).
class Expense:
def __init__(self, cost):
self.cost = cost
def reimburse(self):
return self.cost * 0.8
class Trip:
def __init__(self, cost, start_date, end_date):
self.cost = cost
self.start_date = start_date
self.end_date = end_date
def reimburse(self):
return self.cost * 0.8
class EquipmentOrder:
def __init__(self, cost, item):
self.cost = cost
self.item = item
def reimburse(self):
return self.cost * 0.8
what do Trip and EquipmentOrder inherit from after refactoring?
class Trip(Expense):
def __init__(self, cost, start_date, end_date):
super().__init__(cost)
self.start_date = start_date
self.end_date = end_date
class EquipmentOrder(Expense):
def __init__(self, cost, item):
super().__init__(cost)
self.item = itemExpense — because the shared attributes (cost) and shared method (reimburse()) were moved into a new parent class.
How does it mean to override a method?
a subclass provides its own version of a method that replaces the parent’s version.
why override methods?
to customize behavior for the subclass
which built‑in methods are commonly overridden?
Without overriding:
class Student:
def __init__(self, name):
self.name = name
s = Student("Rebecca")
print(s)
Output:
Code
<__main__.Student object at 0x...>
Override it:
python
class Student:
def __init__(self, name):
self.name = name
def __str__(self):
return f"Student: {self.name}"__str__, __eq__, __hash__.
What does __str__ control?
the string representation of an object (what prints when you call print(obj))
why override __str__?
the default prints a memory address, which is not useful
What does it mean to override a method?
def __str__(self):
return str(self.year)A subclass replaces a method from its parent class with its own version.
the code overrides the parent’s __str__ method
what does __eq__ control?
how 2 objects are compared for equality
What does __hash__ control?
How objects behave in sets and as dictionary keys — it determines their hash value, which Python uses to check equality and store them efficiently
because __hash__ tells Python: Objects with the same UID should be treated as the same item in a set or dict.
Why must __eq__ and __hash__ be consistent?
Objects that are equal must have the same hash value.
should a conference object inherit from Trip or Expense?
it depeds—conference is-a Trip if it shares all Trip behavior; otherwise inherit from Expense
Should a Party (with only a start_date) inherit from Trip (which has start_date and end_date)?
Probably not.
Why? Because inheritance means “is‑a”.
A Party is not a Trip if it doesn’t have all the attributes/behaviors a Trip has.
it’s better to inherit from Expense.
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def give_raise(self, amount):
self.salary += amount
class Contractor:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def give_raise(self, amount):
self.salary += amountwhat shared attributes did Employee and Contractor have?
e = Employee("Rebecca", 60000)
c = Contractor("Jordan", 45000)e.name # "Rebecca"
e.salary # 60000
e.give_raise # method
c.name # "Jordan"
c.salary # 45000
c.give_raise # methodname, salary, and give_raise()

what was the refactoring solution?
create a Worker parent class with shared constructor + give_raise()
