Single inheritance vs. multiple inheritance
Hey there, future coding wizard! Today, we’re going to dive into the magical world of inheritance in Python. Imagine you have a treasure chest full of different kinds of toys, and you want to organize them neatly. Well, that’s what inheritance does with code! It helps us organize our code neatly by allowing one class to inherit attributes and methods from another.
Now, let’s talk about two special types of inheritance: Single Inheritance and Multiple Inheritance.
1. Single Inheritance:
Think of a single inheritance like a tree with branches. You have one main branch (the parent class), and from that branch, you have smaller branches (the child classes) coming out.
Let’s see some Python code to understand this better:
# Parent class
class Animal:
def speak(self):
return "I can speak!"
# Child class inheriting from Animal
class Dog(Animal):
def bark(self):
return "Woof!"
# Creating an instance of Dog
my_dog = Dog()
# Accessing methods from both classes
print(my_dog.speak()) # Output: I can speak!
print(my_dog.bark()) # Output: Woof!
Here, the Dog
class is like a smaller branch coming out of the main branch Animal
. It inherits the speak()
method from the Animal
class.
2. Multiple Inheritance:
Now, imagine you have two main branches, and you want to create a new branch that combines the features of both. That’s multiple inheritance for you!
Let’s look at another example:
# Parent class 1
class Flyable:
def fly(self):
return "I can fly!"
# Parent class 2
class Swimmable:
def swim(self):
return "I can swim!"
# Child class inheriting from both Flyable and Swimmable
class Duck(Flyable, Swimmable):
pass
# Creating an instance of Duck
my_duck = Duck()
# Accessing methods from both parent classes
print(my_duck.fly()) # Output: I can fly!
print(my_duck.swim()) # Output: I can swim!
In this example, the Duck
class inherits from both Flyable
and Swimmable
classes. So, it can both fly and swim!
Practice Questions:
- What is inheritance in Python?
- Explain single inheritance with an example.
- How does multiple inheritance differ from single inheritance?
- Create a class
Car
that inherits fromVehicle
and has a methoddrive()
.
Solutions:
- Inheritance in Python allows a class (child class) to inherit attributes and methods from another class (parent class).
- Single inheritance involves one parent class and one child class. Example:
class Dog(Animal):
. - Multiple inheritance involves more than one parent class. Example:
class Duck(Flyable, Swimmable):
.
# Parent class
class Vehicle:
def start(self):
return "Vehicle started!"
# Child class inheriting from Vehicle
class Car(Vehicle):
def drive(self):
return "Car is driving!"
# Creating an instance of Car
my_car = Car()
# Accessing methods from both classes
print(my_car.start()) # Output: Vehicle started!
print(my_car.drive()) # Output: Car is driving!
Now, off you go, little coder! Practice your magic with inheritance in Python, and soon you’ll be creating amazing programs like a true wizard!