Advertisement
c0d3dsk1lls

ABSTRACT CLASS/ABSTRACT METHOD CodedSkills.net

Aug 3rd, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. # PREVENT A USER FROM CREATING AN OBJECT OF THAT CLASS
  2. # + COMPELLS A USER TO OVERRIDE ABSYRACT METHODS IN A CHILD CLASS
  3.  
  4. #ABSTRACT CLASS = A CLASS WHICH CONTAINS ONE OR MORE ABSTRACT METHODS
  5. #ABSTRACT METHOD = A METHOD THAT HAS A DECLARATION BUT DOES NOT HAVE AN IMPLEMENTATION
  6. from abc import ABC, abstractmethod
  7.  
  8. class Vehicle(ABC):
  9. #-----------------------------------------
  10.     @abstractmethod
  11.     def go(self):
  12.         pass
  13.  
  14.     def stop(self):
  15.         pass
  16. #------------------------------------------
  17. class Car(Vehicle):
  18.  
  19.     def go(self):
  20.         print("You drive the car")
  21.  
  22.     def stop(self):
  23.         print("This car is stopped")
  24. #------------------------------------------    
  25. class Motorcycle(Vehicle):
  26.  
  27.     def go(self):
  28.         print("You ride the motorcycle")
  29.     def stop(self):
  30.         print("This motorcycle is stopped")
  31. #-------------------------------------------
  32. #vehicle = Vehicle()
  33. car = Car()
  34. motorcycle = Motorcycle()
  35. #vehicle.go()
  36. car.go()
  37. motorcycle.go()
  38. car.stop()
  39. motorcycle.stop()
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement