Multiple Inheritance
Part of the Object Oriented Programming section of Coddy's Python journey — lesson 20 of 64.
Multiple inheritance allows a class to inherit from more than one parent class, combining functionality from different sources.
Here are two parent classes:
class Animal:
def __init__(self, name):
self.name = name
def eat(self):
return f"{self.name} is eating"
class Flyable:
def fly(self):
return f"{self.name} is flying"Create a child class that inherits from both parents:
class Bird(Animal, Flyable):
def __init__(self, name, species):
super().__init__(name) # Calls Animal's __init__
self.species = species
def sing(self):
return f"{self.name} is singing"The syntax class Bird(Animal, Flyable): means Bird inherits from both Animal and Flyable.
Create a bird object and use methods from both parents:
sparrow = Bird("Sparrow", "House sparrow")Call methods from the first parent class:
print(sparrow.eat())Call methods from the second parent class:
print(sparrow.fly())Call the bird's own method:
print(sparrow.sing())Output:
Sparrow is eating
Sparrow is flying
Sparrow is singingYou can check the inheritance order:
print(Bird.__mro__) # Method Resolution OrderThis shows which parent gets checked first when looking for methods.
Key Point: Multiple inheritance uses class Child(Parent1, Parent2): syntax. The child class gets all methods from all parent classes. Python checks parents from left to right when looking for methods.
Challenge
MediumIn this challenge, you'll implement a multi-file class system demonstrating multiple inheritance with smartphones. Each class is organized in its own file for better code organization.
You'll need to edit these files:
device.py- Implement theDevicebase classinternet.py- Implement theInternetbase classsmartphone.py- Implement theSmartphoneclass that inherits from both base classesdriver.py- Implement test cases that will verify your implementation works correctly
Each file contains detailed TODO comments that will guide you step-by-step through the implementation.
Cheat sheet
Multiple inheritance allows a class to inherit from more than one parent class using the syntax class Child(Parent1, Parent2):
class Animal:
def __init__(self, name):
self.name = name
def eat(self):
return f"{self.name} is eating"
class Flyable:
def fly(self):
return f"{self.name} is flying"
class Bird(Animal, Flyable):
def __init__(self, name, species):
super().__init__(name) # Calls Animal's __init__
self.species = species
def sing(self):
return f"{self.name} is singing"The child class inherits all methods from all parent classes:
sparrow = Bird("Sparrow", "House sparrow")
print(sparrow.eat()) # From Animal class
print(sparrow.fly()) # From Flyable class
print(sparrow.sing()) # Bird's own methodCheck the Method Resolution Order (MRO) to see which parent gets checked first:
print(Bird.__mro__) # Shows inheritance orderTry it yourself
# TODO: Import the Smartphone class from smartphone.py
# Create a smartphone and test its methods
# TODO: Create a Smartphone object with brand "Apple" and model "iPhone 13"
my_phone = None
# TODO: Call and print the power_on() method of the smartphone
# TODO: Call and print the connect() method of the smartphone
# TODO: Call and print the make_call() method of the smartphone with parameter "123-456-7890"This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self ParameterMethodsAttributesConstructor Method (__init__)Recap - Simple Calculator4Inheritance
Basic InheritanceThe super() FunctionMethod OverridingMultiple InheritanceMethod Resolution OrderRecap - Employee Hierarchy7Special Methods
Magic Methods IntroductionOperator OverloadingContainer Magic MethodsRecap - Custom List10Design Patterns Part 1
Intro to design patternSingleton PatternFactory PatternObserver PatternStrategy Pattern2Decorators
Introduction to DecoratorsProperty DecoratorStatic Method DecoratorClass Method Decorator5Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceMixinsStatic and Class MethodsClass DecoratorsContext Managers3Class Properties
Instance vs Class VariablesProperty DecoratorsPrivate AttributesRecap - Bank Account Manager6Encapsulation
Public, Protected, Private MemAccess ModifiersInformation HidingProperty Decorators AdvancedRecap - Student Records System12Project: Library Management
Project OverviewBook and User Classes