Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Inheritance Pythoonn - 063943

The document shows examples of inheritance in Python using classes. Several base classes like Shape, Vehicle, Employee are defined with common attributes and methods. Derived classes like Circle, Rectangle, Car, Motorcycle extend the base classes by adding more attributes and overriding methods while reusing attributes and methods from the parent classes. Instances are created of the derived classes to demonstrate accessing attributes and calling methods.

Uploaded by

Abdul rauf Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Inheritance Pythoonn - 063943

The document shows examples of inheritance in Python using classes. Several base classes like Shape, Vehicle, Employee are defined with common attributes and methods. Derived classes like Circle, Rectangle, Car, Motorcycle extend the base classes by adding more attributes and overriding methods while reusing attributes and methods from the parent classes. Instances are created of the derived classes to demonstrate accessing attributes and calling methods.

Uploaded by

Abdul rauf Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

class Animal:

def __init__(self, name):


self.name = name

def speak(self):
raise NotImplementedError("Subclass must implement this
method")

class Dog(Animal):
def speak(self):
return "Woof!"

class Cat(Animal):
def speak(self):
return "Meow!"

# Creating instances of derived classes


dog = Dog("Buddy")
cat = Cat("Whiskers")
# Calling the speak() method
print(dog.speak()) # Output: Woof!
print(cat.speak()) # Output: Meow!

2
class Vehicle:
def __init__(self, brand):
self.brand = brand

def drive(self):
print("Driving a", self.brand)

class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand)
self.model = model

def drive(self):
print("Driving a", self.brand, self.model)

class ElectricCar(Car):
def __init__(self, brand, model, battery_capacity):
super().__init__(brand, model)
self.battery_capacity = battery_capacity

def charge(self):
print("Charging the", self.brand, self.model, "with a battery
capacity of", self.battery_capacity, "kWh")

# Creating instances of derived classes


vehicle = Vehicle("Generic")
car = Car("Toyota", "Corolla")
electric_car = ElectricCar("Tesla", "Model S", 75)

# Calling the methods


vehicle.drive() # Output: Driving a Generic
car.drive() # Output: Driving a Toyota Corolla
electric_car.drive() # Output: Driving a Tesla Model S
electric_car.charge() # Output: Charging the Tesla Model S with a
battery capacity of 75 kWh

class Shape:
def __init__(self, color):
self.color = color

def area(self):
pass

def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, color, length, width):
super().__init__(color)
self.length = length
self.width = width

def area(self):
return self.length * self.width

def perimeter(self):
return 2 * (self.length + self.width)

class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color)
self.radius = radius

def area(self):
return 3.14 * self.radius ** 2
def perimeter(self):
return 2 * 3.14 * self.radius

# Creating instances of derived classes


rectangle = Rectangle("Blue", 5, 3)
circle = Circle("Red", 7)

# Calling the methods


print(rectangle.area()) # Output: 15
print(rectangle.perimeter()) # Output: 16
print(circle.area()) # Output: 153.86
print(circle.perimeter()) # Output: 43.96

class Account:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance

def deposit(self, amount):


self.balance += amount
print(f"Deposited {amount} into Account {self.account_number}.
New balance: {self.balance}")

def withdraw(self, amount):


if self.balance >= amount:
self.balance -= amount
print(f"Withdrew {amount} from Account
{self.account_number}. New balance: {self.balance}")
else:
print(f"Insufficient funds in Account {self.account_number}")

def display_balance(self):
print(f"Account {self.account_number} balance: {self.balance}")

class SavingsAccount(Account):
def __init__(self, account_number, balance=0, interest_rate=0.01):
super().__init__(account_number, balance)
self.interest_rate = interest_rate

def calculate_interest(self):
interest = self.balance * self.interest_rate
self.deposit(interest)
print(f"Interest of {interest} credited to Account
{self.account_number}")

class CheckingAccount(Account):
def __init__(self, account_number, balance=0, transaction_limit=3):
super().__init__(account_number, balance)
self.transaction_limit = transaction_limit
self.transactions = 0

def withdraw(self, amount):


if self.transactions < self.transaction_limit:
super().withdraw(amount)
self.transactions += 1
print(f"Transaction {self.transactions}/{self.transaction_limit}")
else:
print("Transaction limit exceeded")
# Creating instances of derived classes
savings_account = SavingsAccount("SA001", 1000, 0.02)
checking_account = CheckingAccount("CA001", 500, 2)

# Performing operations
savings_account.display_balance() # Output: Account SA001
balance: 1000
savings_account.deposit(500) # Output: Deposited 500 into
Account SA001. New balance: 1500
savings_account.calculate_interest() #

class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary

def calculate_bonus(self):
bonus = self.salary * 0.1
return bonus

class Manager(Employee):
def __init__(self, name, salary, department):
super().__init__(name, salary)
self.department = department

def calculate_bonus(self):
bonus = self.salary * 0.15
return bonus

class Developer(Employee):
def __init__(self, name, salary, programming_language):
super().__init__(name, salary)
self.programming_language = programming_language
# Creating instances of derived classes
employee = Employee("John Doe", 50000)
manager = Manager("Jane Smith", 80000, "Marketing")
developer = Developer("Sara Johnson", 60000, "Python")

# Calling the methods


print(employee.calculate_bonus()) # Output: 5000.0
print(manager.calculate_bonus()) # Output: 12000.0
print(developer.calculate_bonus()) # Output: 6000.0

class Vehicle:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def start(self):
print("Engine started.")

def stop(self):
print("Engine stopped.")

class Car(Vehicle):
def __init__(self, make, model, year, num_doors):
super().__init__(make, model, year)
self.num_doors = num_doors

def drive(self):
print(f"{self.make} {self.model} is driving.")

class Motorcycle(Vehicle):
def __init__(self, make, model, year):
super().__init__(make, model, year)

def ride(self):
print(f"{self.make} {self.model} is riding.")

# Creating instances of derived classes


car = Car("Toyota", "Camry", 2022, 4)
motorcycle = Motorcycle("Honda", "CBR500R", 2021)

# Calling the methods


car.start() # Output: Engine started.
car.drive() # Output: Toyota Camry is driving.
car.stop() # Output: Engine stopped.

motorcycle.start() # Output: Engine started.


motorcycle.ride() # Output: Honda CBR500R is riding.
motorcycle.stop() # Output: Engine stopped.

class Shape:
def __init__(self, color):
self.color = color

def get_color(self):
return self.color

def calculate_area(self):
pass

class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color)
self.radius = radius

def calculate_area(self):
return 3.14 * self.radius ** 2

class Rectangle(Shape):
def __init__(self, color, length, width):
super().__init__(color)
self.length = length
self.width = width

def calculate_area(self):
return self.length * self.width

# Creating instances of derived classes


circle = Circle("Red", 5)
rectangle = Rectangle("Blue", 4, 6)

# Accessing attributes and calling methods


print("Circle color:", circle.get_color()) # Output: Circle color:
Red
print("Circle area:", circle.calculate_area()) # Output: Circle area:
78.5

print("Rectangle color:", rectangle.get_color()) # Output: Rectangle


color: Blue
print("Rectangle area:", rectangle.calculate_area()) # Output:
Rectangle area: 24
8

class Employee:
def __init__(self, name, emp_id):
self.name = name
self.emp_id = emp_id

def display_details(self):
print(f"Employee Name: {self.name}")
print(f"Employee ID: {self.emp_id}")

class Manager(Employee):
def __init__(self, name, emp_id, department):
super().__init__(name, emp_id)
self.department = department
def display_details(self):
super().display_details()
print(f"Department: {self.department}")

class Developer(Employee):
def __init__(self, name, emp_id, programming_language):
super().__init__(name, emp_id)
self.programming_language = programming_language

def display_details(self):
super().display_details()
print(f"Programming Language: {self.programming_language}")

# Creating instances of derived classes


manager = Manager("John Doe", 1001, "HR")
developer = Developer("Jane Smith", 2001, "Python")

# Calling the methods


print("Manager Details:")
manager.display_details()
print()

print("Developer Details:")
developer.display_details()

class Shape:
def __init__(self, color):
self.color = color

def get_color(self):
return self.color

def calculate_area(self):
pass
class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color)
self.radius = radius

def calculate_area(self):
return 3.14 * self.radius ** 2

class Rectangle(Shape):
def __init__(self, color, length, width):
super().__init__(color)
self.length = length
self.width = width

def calculate_area(self):
return self.length * self.width

# Creating instances of derived classes


circle = Circle("Red", 5)
rectangle = Rectangle("Blue", 4, 6)
# Accessing attributes and calling methods
print("Circle color:", circle.get_color()) # Output: Circle color:
Red
print("Circle area:", circle.calculate_area()) # Output: Circle area:
78.5

print("Rectangle color:", rectangle.get_color()) # Output: Rectangle


color: Blue
print("Rectangle area:", rectangle.calculate_area()) # Output:
Rectangle area: 24

10

You might also like