Class and Object Relationship
Classes define blueprints for creating objects. Objects possess attributes (state) and methods (behavior).
Implementing Object-Oriented Concepts
Class Definition
class Vehicle():
pass
Object Instantiation
car = Vehicle()
print(car) # Memory address
Self Parameter
class Vehicle():
def start_engine(self):
print("Engine started")
print(self)
car = Vehicle()
print(car) # Same address as self
Multiple Objects
vehicle1 = Vehicle()
vehicle2 = Vehicle()
# Different memory addresses
Object Attributes
Adding Attributes Externally
vehicle1.max_speed = 200
vehicle1.fuel_capacity = 50
Accessing Attributes Internally
class Vehicle():
def display_specs(self):
print(f"Max speed: {self.max_speed}")
print(f"Fuel capacity: {self.fuel_capacity}")
vehicle1 = Vehicle()
vehicle1.max_speed = 200
vehicle1.fuel_capacity = 50
vehicle1.display_specs()
Special Methods
__init__ Method
class Vehicle():
def __init__(self):
self.max_speed = 180
self.fuel_capacity = 45
Parameterized __init__
class Vehicle():
def __init__(self, speed, capacity):
self.max_speed = speed
self.fuel_capacity = capacity
car = Vehicle(200, 50)
__str__ Method
class Vehicle():
def __str__(self):
return "Vehicle specifications"
car = Vehicle()
print(car) # Vehicle specifications
__del__ Method
class Vehicle():
def __del__(self):
print("Object destroyed")
Inheritance
Single Inheritance
class Engine():
def __init__(self):
self.type = "V8"
class Car(Engine):
pass
my_car = Car()
print(my_car.type) # V8
Mulitple Inheritance
class ElectricMotor():
def __init__(self):
self.power_source = "Electric"
class CombustionEngine():
def __init__(self):
self.power_source = "Gasoline"
class HybridVehicle(ElectricMotor, CombustionEngine):
pass
hybrid = HybridVehicle()
print(hybrid.power_source) # Electric
Method Overriding
class HybridVehicle(ElectricMotor, CombustionEngine):
def __init__(self):
self.power_source = "Hybrid"
def show_power(self):
print(f"Power: {self.power_source}")
hybrid = HybridVehicle()
hybrid.show_power() # Power: Hybrid
Accessing Parent Methods
class HybridVehicle(ElectricMotor, CombustionEngine):
def show_electric(self):
ElectricMotor.__init__(self)
print(f"Source: {self.power_source}")
def show_combustion(self):
CombustionEngine.__init__(self)
print(f"Source: {self.power_source}")
hybrid = HybridVehicle()
hybrid.show_electric() # Source: Electric
Super() Function
class HybridVehicle(ElectricMotor):
def __init__(self):
super().__init__()
# Additional initialization
Access Control
class Vehicle():
def __init__(self):
self.__vin = "ABC123"
def get_vin(self):
return self.__vin
def set_vin(self, new_vin):
self.__vin = new_vin
car = Vehicle()
print(car.get_vin()) # ABC123
car.set_vin("XYZ789")
OOP Principles
Encapsulation: Bundling data and methods within a class
Inheritance: Creating new classes from existing ones
Polymorhpism: Different objects responding to same method differently
Polymorphism Example
class PaymentMethod():
def process_payment(self):
pass
class CreditCard(PaymentMethod):
def process_payment(self):
print("Processing credit card")
class PayPal(PaymentMethod):
def process_payment(self):
print("Processing PayPal")
def execute_payment(method):
method.process_payment()
card = CreditCard()
paypal = PayPal()
execute_payment(card) # Processing credit card
execute_payment(paypal) # Processing PayPal
Class vs Instance Attributes
class Vehicle():
wheels = 4 # Class attribute
car = Vehicle()
bike = Vehicle()
print(Vehicle.wheels) # 4
print(car.wheels) # 4
Vehicle.wheels = 6
print(bike.wheels) # 6
car.wheels = 2 # Creates instance attribute
print(car.wheels) # 2
print(Vehicle.wheels) # 6
Class and Static Methods
class Vehicle():
@classmethod
def class_info(cls):
print("Vehicle class information")
@staticmethod
def static_info():
print("Static vehicle info")
Vehicle.class_info()
Vehicle.static_info()