Single Inheritance in Python

Dog(Animal) can speak() without defining it. That is the whole inheritance pitch.

BeginnerObject-Oriented ProgramsExample 4 of 25
single-inheritance.py
1# Program to demonstrate single inheritance
2
3class Animal:
4 def speak(self):
5 print("Animal makes a sound")
6
7class Dog(Animal):
8 def bark(self):
9 print("Dog barks")
10
11dog = Dog()
12dog.speak()
13dog.bark()

Output

Animal makes a sound
Dog barks

What's going on

Dog inherits from Animal, so instances of Dog can call both 'speak' and 'bark'.

After this program

If the idea is still fuzzy, read Day 11: Classes and Objects in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 object-oriented programs, or see every topic on Python exercises.