class Car:
def __init__(self, color):
self.color = color
def drive(self):
return f"The {self.color} car is driving!"
# ElectricCar inherits from Car
class ElectricCar(Car):
def charge(self):
return f"The {self.color} electric car is charging."
my_tesla = ElectricCar("blue")
print(my_tesla.drive()) # inherited from Car
print(my_tesla.charge()) # unique to ElectricCar
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter