Introduction
Multiple inheritance is a feature in object-oriented programming where a class (called the child or subclass) can inherit attributes and methods from more than one parent class (or superclass). This allows the subclass to combine and reuse the functionality of multiple classes.
Key Concepts
Parent Classes (Superclasses)
The classes whose attributes and methods are inherited by another class.
Child Class (Subclass)
The class that inherits attributes and methods from multiple parent classes.
Multiple Inheritance
Description: A single class (child class) inherits from multiple parent classes.
+---------+ +---------+
| Class A | | Class B |
+---------+ +---------+
\ /
\ /
+---------+
| Class C |
+---------+
Syntax
Defining Parent Classes
class ParentClass1:
def __init__(self, attribute1):
self.attribute1 = attribute1
def method1(self):
return "This is method1 from ParentClass1"
class ParentClass2:
def __init__(self, attribute2):
self.attribute2 = attribute2
def method2(self):
return "This is method2 from ParentClass2"
Defining a Child Class
class ChildClass(ParentClass1, ParentClass2):
def __init__(self, attribute1, attribute2, attribute3):
ParentClass1.__init__(self, attribute1) # Inheriting from ParentClass1
ParentClass2.__init__(self, attribute2) # Inheriting from ParentClass2
self.attribute3 = attribute3
def method3(self):
return "This is method3 from ChildClass"
Example
Let’s consider a real-world example of a person who is both a teacher and an athlete, where the Teacher
and Athlete
classes are the parent classes.
Parent Classes: Teacher
and Athlete
class Teacher:
def __init__(self, subject):
self.subject = subject
def teach(self):
return f"Teaching {self.subject}"
class Athlete:
def __init__(self, sport):
self.sport = sport
def play(self):
return f"Playing {self.sport}"
Child Class: Person
class Person(Teacher, Athlete):
def __init__(self, name, subject, sport):
Teacher.__init__(self, subject) # Inheriting from Teacher
Athlete.__init__(self, sport) # Inheriting from Athlete
self.name = name
def display_info(self):
return f"Person: {self.name}, Subject: {self.subject}, Sport: {self.sport}"
Creating and Using Objects
# Creating an object of the Person class
person = Person("Ramesh", "Mathematics", "Cricket")
# Displaying information and using inherited methods
print(person.display_info()) # Output: Person: Ramesh, Subject: Mathematics, Sport: Cricket
print(person.teach()) # Output: Teaching Mathematics
print(person.play()) # Output: Playing Cricket
Explanation
- Parent Class (
Teacher
): Defines an attribute (subject
) and a method (teach
). - Parent Class (
Athlete
): Defines an attribute (sport
) and a method (play
). - Child Class (
Person
): Inherits from bothTeacher
andAthlete
, combining their attributes and methods. It also defines an additional attribute (name
) and a method (display_info
).
Real-World Example: Electronic Devices
Let’s consider a real-world example of an electronic device system where we have a generic Gadget
class and more specific classes like Phone
and Camera
.
Parent Classes: Phone
and Camera
class Phone:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def call(self):
return f"Calling from {self.brand} {self.model}"
class Camera:
def __init__(self, megapixels):
self.megapixels = megapixels
def take_photo(self):
return f"Taking photo with {self.megapixels}MP camera"
Child Class: SmartPhone
class SmartPhone(Phone, Camera):
def __init__(self, brand, model, megapixels):
Phone.__init__(self, brand, model) # Inheriting from Phone
Camera.__init__(self, megapixels) # Inheriting from Camera
def display_info(self):
return f"SmartPhone: {self.brand} {self.model}, Camera: {self.megapixels}MP"
Creating and Using Objects
# Creating an object of the SmartPhone class
smartphone = SmartPhone("Apple", "iPhone 13", 12)
# Displaying information and using inherited methods
print(smartphone.display_info()) # Output: SmartPhone: Apple iPhone 13, Camera: 12MP
print(smartphone.call()) # Output: Calling from Apple iPhone 13
print(smartphone.take_photo()) # Output: Taking photo with 12MP camera
Explanation
- Parent Class (
Phone
): Defines common attributes (brand
,model
) and a method (call
) for all phones. - Parent Class (
Camera
): Defines an attribute (megapixels
) and a method (take_photo
) for all cameras. - Child Class (
SmartPhone
): Inherits from bothPhone
andCamera
, combining their attributes and methods. It also defines an additional method (display_info
).
Conclusion
Multiple inheritance in Python allows a child class to inherit attributes and methods from more than one parent class. This promotes code reusability and allows for more complex class hierarchies. By understanding and utilizing multiple inheritance, you can design more flexible and powerful object-oriented systems. The provided examples of a person who is both a teacher and an athlete, and a smartphone combining features of a phone and a camera, demonstrate how multiple inheritance can be effectively applied in real-world scenarios.