Advertisement
GamerBhai02

APP Exp 14

May 29th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | Source Code | 0 0
  1. from abc import ABC, abstractmethod
  2. class Animal(ABC):
  3.     def sound(self):
  4.         pass
  5.     def common_behavior(self):
  6.         print("All animals need food and water")
  7. class Dog(Animal):
  8.     def sound(self):
  9.         return "Bark"
  10. class Cat(Animal):
  11.     def sound(self):
  12.         return "Meow"
  13. dog = Dog()
  14. cat = Cat()
  15. print(f"Dog says: {dog.sound()}")
  16. print(f"Cat says: {cat.sound()}")
  17. class Vehicle(ABC):
  18.     @abstractmethod
  19.     def start_engine(self):
  20.         pass
  21.     def stop_engine(self):
  22.         pass
  23. class Car(Vehicle):
  24.     def start_engine(self):
  25.         print("Car engine started")
  26.     def stop_engine(self):
  27.         print("Car engine stopped")
  28. class Bike(Vehicle):
  29.     def start_engine(self):
  30.         print("Bike engine started")
  31.     def stop_engine(self):
  32.         print("Bike engine stopped")
  33. car = Car()
  34. bike = Bike()
  35. car.start_engine()
  36. car.stop_engine()
  37. bike.start_engine()
  38. bike.stop_engine()
  39. from abc import ABC, abstractmethod
  40. class Shape(ABC):
  41.     @abstractmethod
  42.     def area(self):
  43.         pass
  44. class Square(Shape):
  45.     def __init__(self, side_length):
  46.         self.side_length = side_length
  47.     def area(self):
  48.         return self.side_length ** 2
  49. class Rectangle(Shape):
  50.     def __init__(self, width, height):
  51.         self.width = width
  52.         self.height = height
  53.     def area(self):
  54.         return self.width * self.height
  55. square = Square(4)
  56. rectangle = Rectangle(4, 6)
  57. print(f"Area of Square: {square.area()}")
  58. print(f"Area of Rectangle: {rectangle.area()}")
Tags: exp 14
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement