Inheritance and Polymorphism
Inheritance:
class Vehicle:
vehicle_counter = 0
def __init__(self, body_type, make):
self.vehicle_body = body_type
self.vehicle_make = make
Vehicle.vehicle_counter += 1
def get_vehicle_count(self):
retunr Vehicle.vehicle_counter
class Truck(Vehicle):
# By specifying Vehicle in parantheses the truck class iherits the propreties of the vehicle class
# So to create a truck you would use
truck1 = Truck('big rig', 'mercedes')
truck2 = Truck('small rig', 'Chevy')
truck3 = Truck('big rig', 'toyota')
truck3.get_vehicle_count # This gets the actual count of vehiclesBased on the
Truckclass definition it is aVehicleThis increments the
Vehiclecounter accordinglyTruckclass must be used the same way as specified in the constructor of theVehicleclassTruckclass inherits theVehiclefunctionality
Polymorphism (or Method Overwriting):
The
Truckclass is also called achild-classor asub-classof aclassThe
Vehicleclass is also called abase-classor aparent-class
Last updated