Python How to inherit a class, Inheritance Basics - Python programming for beginners.

Published: 01 December 2022
on channel: Brendan Callaghan
160
2

Python Inheritance - In this video I discuss the basics of inheritance in python and how it works
Music:-
Music by Bensound, www.bensound.com

class Vehicle:
def __init__(self, name, speed):
self.name = name
self.speed = speed

def acceleration(self):
self.speed += 5

def brake(self):
self.speed -= 5


class Car(Vehicle):
def __init__(self, name, speed, engine_capacity):
Vehicle.__init__(self, name, speed)
self.engine_capacity = engine_capacity

def display_car(self):
print(f"The cars name is {self.name}")
print(f"The cars speed is {self.speed}")
print(f"The cars engine capacity is {self.engine_capacity}")


class Truck(Vehicle):
def __init__(self, name, speed, engine_capacity):
Vehicle.__init__(self, name, speed)
self.engine_capacity = engine_capacity

def display_truck(self):
print(f"The trucks name is {self.name}")
print(f"The trucks speed is {self.speed}")
print(f"The trucks engine capacity is {self.engine_capacity}")

from vehicle import Car, Truck

car = Car("Lightening McQueen", 200, 2000)
truck = Truck("Fire Truck",100, 3500)

car.display_car()
car.acceleration()
car.acceleration()
car.brake()
car.display_car()

truck.display_truck()
truck.brake()
truck.display_truck()


On this page of the site you can watch the video online Python How to inherit a class, Inheritance Basics - Python programming for beginners. with a duration of hours minute second in good quality, which was uploaded by the user Brendan Callaghan 01 December 2022, share the link with friends and acquaintances, this video has already been watched 160 times on youtube and it was liked by 2 viewers. Enjoy your viewing!