Lesson 26 of 55
PythonOOP – Inheritance
20 minutes
Course progress47%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 26 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
Inheritance lets you create a subclass that reuses and extends the behavior of a base class.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError
class Dog(Animal):
def speak(self):
return 'Woof!'
class Cat(Animal):
def speak(self):
return 'Meow.'
pet = Dog('Rex')
print(pet.speak())
The Dog and Cat classes inherit __init__ from Animal and provide their own speak implementation.