Python Practice › OOP Programs › Program 10

Multiple inheritance

EasyOOP Programs · 10 of 48

Write a Python program that demonstrates multiple inheritance.

Advertisement
Output
photo taken | calling
['SmartPhone', 'Camera', 'Phone', 'object']

Approach

  1. A class inherits from two parents; the MRO decides lookup order.

Solution

Python
class Camera:    def photo(self):        return "photo taken" class Phone:    def call(self):        return "calling" class SmartPhone(Camera, Phone):    pass s = SmartPhone()print(s.photo(), "|", s.call())print([c.__name__ for c in SmartPhone.__mro__])
Output
photo taken | calling
['SmartPhone', 'Camera', 'Phone', 'object']

Line by line

Line 13

The method resolution order shows Python searches SmartPhone, then Camera, then Phone.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Leave a Comment