ds 110 - lecture 18

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:18 AM on 3/25/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

31 Terms

1
New cards

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)

2
New cards

why should you use inheritance?

to avoid duplicated code, share common functionality, and make code easier to maintain

3
New cards

what is a base class?

the class being inherited from

4
New cards
Subclass = child class
Superclass = parent class

Subclass inherits:
    ✔ attributes
    ✔ methods
    ✔ behavior

what is a subclass?

a class that inherits from a base class

5
New cards

“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

6
New cards
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.)

7
New cards

why is isinstance() useful?

to prevent bugs by ensuring the correct object type is passed

8
New cards

what does super() do?

gives access to the parent class’s methods

9
New cards

why use super() in a constructor?

to reuse initialization code from the parent class

10
New cards

How do you call a parent class constructor in a subclass?

Use super().__init__(arg1, arg2, ...) with the same arguments the parent expects.

11
New cards

if A inherits from B, and B inherits from C, what does A inherit?

everything from B and C

12
New cards
<p>if Firstyear inherits from Student, which inherits from Client, what methods does Firstyear have?</p>

if Firstyear inherits from Student, which inherits from Client, what methods does Firstyear have?

all methods from Firstyear, Student, and Client

13
New cards

what is the “is-a” rule?

a subclass should represent a more specific version of the parent class

14
New cards

when should you use introduce inheritance?

when you notice duplicated code that can be rafctored into a shared parent class

15
New cards
<p><strong>Why did we create an </strong><code>Expense</code><strong> parent class when refactoring </strong><code>Trip</code><strong> and </strong><code>EquipmentOrder</code><strong>?</strong></p>

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.

16
New cards
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()

17
New cards
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

18
New cards
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 = item

Expense — because the shared attributes (cost) and shared method (reimburse()) were moved into a new parent class.

19
New cards

How does it mean to override a method?

a subclass provides its own version of a method that replaces the parent’s version.

20
New cards

why override methods?

to customize behavior for the subclass

21
New cards

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__.

22
New cards

What does __str__ control?

the string representation of an object (what prints when you call print(obj))

23
New cards

why override __str__?

the default prints a memory address, which is not useful

24
New cards

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

25
New cards

what does __eq__ control?

how 2 objects are compared for equality

26
New cards

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.

27
New cards

Why must __eq__ and __hash__ be consistent?

Objects that are equal must have the same hash value.

28
New cards

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

29
New cards

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.

30
New cards
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 += amount

what 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  # method

name, salary, and give_raise()

31
New cards
<p>what was the refactoring solution?</p>

what was the refactoring solution?

create a Worker parent class with shared constructor + give_raise()

<p>create a Worker parent class with shared constructor + give_raise()</p>

Explore top flashcards

flashcards
Chapter 8 : Jobs
109
Updated 1162d ago
0.0(0)
flashcards
Stage 25
31
Updated 1099d ago
0.0(0)
flashcards
Chapter 1: Sampling and Data
57
Updated 1172d ago
0.0(0)
flashcards
Sosiologi begrep
48
Updated 860d ago
0.0(0)
flashcards
Chapter 8 : Jobs
109
Updated 1162d ago
0.0(0)
flashcards
Stage 25
31
Updated 1099d ago
0.0(0)
flashcards
Chapter 1: Sampling and Data
57
Updated 1172d ago
0.0(0)
flashcards
Sosiologi begrep
48
Updated 860d ago
0.0(0)