-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract.py
More file actions
32 lines (22 loc) · 751 Bytes
/
abstract.py
File metadata and controls
32 lines (22 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Abstract class = A class that cannot be instantiated on it's own, it is meant to be subclassed
# It contains abstract methods that are declared but have no implementation
# Abstract class prevents instantiation of itself
# It requires the children to declare the abstract methods
from abc import ABC, abstractmethod
# Example 1
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
@abstractmethod
def stop(self):
pass
class Car(Vehicle):
def start(self):
print("Car started")
def stop(self):
print("Car stopped")
# car = Car() # TypeError: Can't instantiate abstract class Car without an implementation for abstract methods 'start', 'stop'
car = Car()
car.start()
car.stop()