Inheritance
Hey, kiddo!
Today, we’re going to dive into something super cool called “Inheritance” in Python. Imagine you have a toy box full of different toys. Some are cars, some are dolls, and some are action figures. Now, each of these toys has something special about them, right? Like cars have wheels, dolls can talk, and action figures can move their arms and legs.
In Python, we have something similar called classes. Classes are like blueprints for creating objects, just like how the blueprints of a toy tell the factory workers how to make it. And guess what? With inheritance, we can make new classes that are like the old ones but with some extra features or changes, just like how you might add new features to your toys!
Let’s see some Python code to understand it better:
# Parent class
class Toy:
def __init__(self, name):
self.name = name
def play(self):
print(f"Playing with {self.name}")
# Child class inheriting from Toy
class Car(Toy):
def __init__(self, name, color):
super().__init__(name)
self.color = color
def drive(self):
print(f"{self.name} is driving in {self.color} color")
# Child class inheriting from Toy
class Doll(Toy):
def talk(self):
print(f"{self.name} says: Hi, I'm a doll!")
# Child class inheriting from Car
class RemoteControlCar(Car):
def __init__(self, name, color, remote_type):
super().__init__(name, color)
self.remote_type = remote_type
def control(self):
print(f"Controlling {self.name} with {self.remote_type} remote")
# Creating objects of different classes
my_car = Car("Speedy", "red")
my_doll = Doll("Anna")
my_rc_car = RemoteControlCar("Racer", "blue", "wireless")
# Playing with objects
my_car.play()
my_car.drive()
my_doll.play()
my_doll.talk()
my_rc_car.play()
my_rc_car.drive()
my_rc_car.control()
In this code, we have a Toy
class that is like the base class. Then, we have Car
and Doll
classes which are like specialized versions of Toy
. The RemoteControlCar
class is a special type of Car
that can be controlled remotely.
Now, let’s have some practice questions to see if you’ve got the hang of it:
- What is inheritance in Python?
- Explain with an example how inheritance helps in code reusability.
- How can you call a method from the parent class inside a method of the child class?
- Create a new class called
Superhero
which inherits fromToy
and has a methodfly()
.
And here are the solutions:
- Inheritance in Python is a way to create a new class using an existing class. The new class can inherit the attributes and methods of the existing class, and it can also have its unique attributes and methods.
- Inheritance helps in code reusability by allowing us to create new classes that inherit the behavior of existing classes. Instead of writing the same code again, we can simply inherit from the existing class and add or modify the specific features we need in the new class.
- We can call a method from the parent class inside a method of the child class using the
super()
function. For example,super().parent_method()
will callparent_method()
of the parent class from within the method of the child class. - Here’s the solution for creating the
Superhero
class:
# Child class inheriting from Toy
class Superhero(Toy):
def fly(self):
print(f"{self.name} is flying like a superhero!")
Now you know all about inheritance in Python! Keep practicing and exploring, and soon you'll be a Python superhero yourself! 🚀🐍