Inheritance in python

PHOTO EMBED

Sun Sep 21 2025 20:23:08 GMT+0000 (Coordinated Universal Time)

Saved by @oforey #python

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
content_copyCOPY