Remember
A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.
A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Animal:
def speak(self):
return "sound"
class Dog(Animal):
def speak(self):
return 'woof'
pet = Dog()
print(pet.speak())Top-level scope
class AnimalObject 4 · typeclass DogObject 5 · DogDog({})A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Animal:
def speak(self):
return "sound"
class Dog(Animal):
def speak(self):
return 'woof'
pet = Dog()
print(pet.speak())
|
v
DISPLAYED RESULT
woof
FINAL BINDINGS
Animal --> object 2 (type)
Dog --> object 4 (type)
pet --> object 5 (Dog)
object 2: class Animal
object 4: class Dog
object 5: Dog({})Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Animal:
def speak(self):
return "sound"
class Dog(Animal):
def speak(self):
return 'woof'
pet = Dog()
print(pet.speak())Top-level scope
class AnimalObject 4 · typeclass DogObject 5 · DogDog({})The example has finished. Compare the final output with the values and references above.
woof
What do you expect this program to print?
class Animal:
def speak(self):
return "sound"
class Dog(Animal):
def speak(self):
return 'woof'
pet = Dog()
print(pet.speak())In programming, Inheritance is a way to make one class (a blueprint for an object) get all the features (code) of another class — so you don't have to repeat yourself!
This helps us reuse code and build on top of existing code instead of starting from scratch.
A class is like a template or blueprint. It defines how something should behave. We use classes to create objects.
Inheritance means that one class (called the child) can use the code of another class (called the parent).
Let’s say we have a general class called Animal that has a function speak(). We can create a class Dog that inherits everything from Animal.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
pass
d = Dog()
d.speak()
Animal speaks
Animal is the parent class with a method speak().Dog is the child class — it uses pass which means "don’t add anything new right now."d as a Dog object, but it can still use the speak() method from Animal.class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
d.speak()
d.bark()
Animal speaks
Dog barks
Dog gets speak() from Animal.Dog also has its own method bark().class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog says Woof!")
d = Dog()
d.speak()
Dog says Woof!
Dog has its own speak() method.speak() from the Animal class.d.speak(), it uses Dog’s version instead.Inheritance helps us build smarter programs by reusing and extending code. Once you understand how a child class can use and even change what it gets from a parent class, you’ll be able to organize your Python programs better and avoid repeating yourself.
Don’t worry if this feels new — with practice, inheritance will feel natural!
Connect what you noticed in the example with the idea below.
A subclass can reuse behavior from its parent and add or override methods. Attribute lookup follows the inheritance hierarchy.
A subclass must rewrite every method of its parent.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.