# Define the Car class
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.is_running = False
def start_engine(self):
self.is_running = True
print(f"The {self.year} {self.make} {self.model}'s engine is now running.")
def stop_engine(self):
self.is_running = False
print(f"The {self.year} {self.make} {self.model}'s engine is now off.")
# Create an object (instance) of the Car class
my_car = Car("Toyota", "Camry", 2022)
# Interact with the object
print(f"I own a {my_car.year} {my_car.make} {my_car.model}.")
my_car.start_engine()
my_car.stop_engine()
class BankAccount:
def __init__(self, owner: str, balance: float):
self.owner = owner
self.__balance = balance # Private attribute
def deposit(self, amount: float):
if amount > 0:
self.__balance += amount
print(f"Deposited ${amount}. New balance: ${self.__balance}")
else:
print("Deposit amount must be positive.")
def withdraw(self, amount: float):
if 0 < amount <= self.__balance:
self.__balance -= amount
print(f"Withdrew ${amount}. Remaining balance: ${self.__balance}")
else:
print("Invalid withdrawal amount.")
def get_balance(self):
return self.__balance # Controlled access to private data
# Usage
account = BankAccount("Alice", 1000)
account.deposit(500)
account.withdraw(200)
# Trying to access private attribute (will cause an error)
# print(account.__balance) # AttributeError
# Parent class
class Vehicle:
def __init__(self, brand: str, wheels: int):
self.brand = brand
self.wheels = wheels
def move(self):
print(f"The {self.brand} vehicle is moving.")
# Child class (inherits from Vehicle)
class Car(Vehicle):
def __init__(self, brand: str, model: str, year: int):
super().__init__(brand, 4) # Car has 4 wheels
self.model = model
self.year = year
def honk(self):
print(f"{self.brand} {self.model} honks: Beep Beep!")
# Usage
my_car = Car("Tesla", "Model 3", 2023)
my_car.move() # Inherited method
my_car.honk() # Child class method
class Animal:
def make_sound(self):
pass # To be implemented by subclasses
class Dog(Animal):
def make_sound(self):
return "Woof!"
class Cat(Animal):
def make_sound(self):
return "Meow!"
# Polymorphic behavior
animals = [Dog(), Cat()]
for animal in animals:
print(animal.make_sound()) # Calls the respective method
from abc import ABC, abstractmethod
# Abstract Base Class
class Shape(ABC):
@abstractmethod
def area(self) -> float:
"""Method to calculate area"""
pass
@abstractmethod
def perimeter(self) -> float:
"""Method to calculate perimeter"""
pass
# Concrete Subclass (inherits from Shape)
class Rectangle(Shape):
def __init__(self, width: float, height: float):
self.width = width
self.height = height
def area(self) -> float:
return self.width * self.height
def perimeter(self) -> float:
return 2 * (self.width + self.height)
# Concrete Subclass (inherits from Shape)
class Circle(Shape):
def __init__(self, radius: float):
self.radius = radius
def area(self) -> float:
return 3.1416 * self.radius ** 2
def perimeter(self) -> float:
return 2 * 3.1416 * self.radius
# Usage
shapes = [Rectangle(10, 5), Circle(7)]
for shape in shapes:
print(f"Area: {shape.area()}, Perimeter: {shape.perimeter()}")