1 of 10

Lecture 13: Inheritance

Autumn 2026

1

Sli.do

#cse163

Questions?

Icebreaker

Favorite candy?

CSE 163

CSE 163: Inheritance

​

​

2 of 10

Announcements

  • Homework 3 due tomorrow, October 29 at 11:59pm on Gradescope
  • Resubmission Form 2 due Sunday, November 1 at 11:59pm
    • Fill out the resubmission form AND re-submit code on Gradescope
    • Homework 1 and Homework 2 eligible (pick one!)
    • Last chance to re-submit Homework 1
    • No form, no regrade
  • Programming Practice 4 due Sunday, November 1 at 11:59pm on Gradescope

2

Questions?

CSE 163: Inheritance

​

​

3 of 10

Review: Components of a Class

3

class SpiderFolk:

​

def __init__(self, name: str) -> None:

self.name: str = name

​

def thwip(self) -> None:

print(self.name + ‘: Thwip!')

​

A class definition

​

An initializer that sets fields (state)

​

​

A method (behavior)

​

CSE 163: Inheritance

​

​

4 of 10

Private in Python

  • Python has no way of enforcing this, but people by convention don’t access anything starting with an underscore (“__”)

4

class SpiderFolk:

def __init__(self, name: str) -> None:

self._name: str = name

​

def thwip(self) -> None:

print(self._name + ': Thwip!')

​

CSE 163: Inheritance

​

​

5 of 10

Value vs. Referential Equality

​

Consider the following code

Compare these lists in two different ways

​

  • Value equality (x == y): compares whether the x and y have the same values

​

  • Referential equality (x is y): compares whether x and y point to the same object

5

l1 = [1, 2, 3]

l2 = [1, 2, 3]

l3 = l1

​

CSE 163: Inheritance

​

​

6 of 10

Defining Value Equality with Objects

  • We will use the __eq__ method to compare the values of two objects!
    • Because we define our objects, we must also define what it means for two objects to be value equivalent!
  • isInstance(other, Class) will check if the object referred to by other is of type Class
    • Use this to check for class compatibility before running any sort of comparison

6

CSE 163: Inheritance

​

​

7 of 10

Special (Dunder) Methods

7

Syntax

Method

x < y

x.__lt__(y)

x > y

x.__gt__(y)

x == y

x.__eq__(y)

x <= y

x.__le__(y)

x >= y

x.__ge__(y)

print(x)

print(x.__str__())

x[i]

x.__getitem__(i)

x[i] = v

x.__setitem__(i, v)

CSE 163: Inheritance

​

​

8 of 10

Lambdas

  • Lambdas allow us to define functions within the body of our code

8

def get_dog_name(d):

return d.get_name()

​

sorted(dogs, key=get_dog_name)

​

sorted(dogs, key=lambda d: d.get_name())

CSE 163: Inheritance

​

​

9 of 10

Inheritance

  • Often multiple classes share common attributes and behaviors
    • There are different types of courses at a university: lecture courses (CSE 163!), lab courses, seminars, capstone courses
    • All share basic attributes: course code, credit hours, instructor
    • Each type of course can have a unique component that distinguishes it from the others
  • Inheritance is an OOP concept that allows us to create a new class (subclass or child class) that gets (or inherits) attributes and methods from an existing class (superclass or parent class)
    • A subclass can inherit from multiple superclasses, but be careful of method calls that share the same name but have different behaviors.

9

CSE 163: Inheritance

​

​

10 of 10

Inheritance Example

10

class Dog:

  def __init__(self, name: str) -> None:

    self._name: str = name

�

  def bark(self) -> None:

    print(self._name + ': Woof')

class ServiceDog(Dog):

  def __init__(self, name: str, service: str) -> None:

super().__init__(name)

​

    self._service: str = service

�

  def bark(self) -> None:

    print(self._name + ‘: *alert bark*')

CSE 163: Inheritance

​

​