Inheritance is an important concept of OOP (Object Oriented Programming). It is a process by which a class inherit the properties and methods from another class (parent class).
The classes which are derived from parent class are called child classes.
For example. let us examine class Vehicle.
class Vehicle:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def drive(self):
print(f"Starting {{0}}".format(self.brand))
def stop(self):
print(f"Stopping {{0}}".format(self.brand))
Now that we have our parent class defined, let's create our child class Car
which inherits class Vehicle
.
class Car(Vehicle):
def __init__(self, brand, color, fuel_type, number_of_seats):
super().__init__(brand, color)
self.fuel_type = fuel_type
self.number_of_seats = number_of_seats
def horn(self):
print("Beep Beep!!")
A lot is going on here.
- First,
class Car(Vehicle):
is used for inheriting a Car class from a parent classVehicle
. - We can use
super()
to call the parent class constructor by passing the required parameters. - As the Car is inheriting the Vehicle class, all the properties and methods defined in Vehicle are now available to Car class.
Creating Objects
Now that we have defined the Car class, let us now create few car objects and see it in action.
volkswagen = Car("Volkswagen", "Red", "Petrol", 5)
volkswagen.drive()
volkswagen.horn()
volkswagen.stop()
print("=======")
tesla = Car("Tesla", "White", "Electric", 5)
tesla.drive()
tesla.horn()
tesla.stop()
Output:
Starting Volkswagen
Beep Beep!!
Stopping Volkswagen
=======
Starting Tesla
Beep Beep!!
Stopping Tesla
Overriding Methods in Python
Method overriding is one of the object-oriented programming feature, that allows subclass to provide a specific implementation of a method that is already defined in its parent class.
When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
One last cool feature of inheritance is the concept of overriding methods. This is creating
Let's see this in action:
class Vehicle:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def drive(self):
print(f"Starting {{0}}".format(self.brand))
def stop(self):
print(f"Stopping {{0}}".format(self.brand))
class Car(Vehicle):
def __init__(self, brand, color, fuel_type, number_of_seats):
super().__init__(brand, color)
self.fuel_type = fuel_type
self.number_of_seats = number_of_seats
def drive(self):
print(f"Starting {{0}}".format(self.brand))
print("Activating auto-pilot mode")
print("Engine rolling")
def horn(self):
print("Beep Beep!!")
volkswagen = Vehicle("Volkswagen", "Red")
volkswagen.drive()
volkswagen.stop()
print("=====")
tesla = Car("Tesla", "White", "Electric", 5)
tesla.drive()
tesla.horn()
tesla.stop()
Output:
Starting Volkswagen
Stopping Volkswagen
=====
Starting Tesla
Activating auto-pilot mode
Engine rolling
Beep Beep!!
Stopping Tesla
Since we defined the drive()
function with the same name as the parent class, Python automatically runs the one in the child.
Inheritance is a great way to organize your code and keep properties and functions where they belong.