-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract class
53 lines (39 loc) · 1.79 KB
/
abstract class
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Abstract Class:-
Definition: A class is considered abstract if it contains one or more abstract methods.
Purpose: Abstract classes serve as templates for other classes. They enforce certain methods to be implemented in derived classes.
Implementation: Abstract classes are created using the ABC (Abstract Base Class) from the abc module.
Instantiation: Abstract classes cannot be instantiated directly.
Abstract Method:-
Definition: A method declared in an abstract class with the @abstractmethod decorator but without any implementation.
Purpose: Abstract methods act as placeholders for methods that must be implemented in subclasses.
Implementation: Subclasses that inherit the abstract class must provide an implementation for all abstract methods.
"""
from abc import ABC, abstractmethod
class Shape(ABC):
# Abstract method
@abstractmethod
def area(self):
pass
# Abstract method
@abstractmethod
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
# Implementing abstract methods
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
# Cannot instantiate Shape (abstract class)
# s = Shape() # Raises TypeError
# Instantiating Rectangle (concrete class)
r = Rectangle(10, 5)
print(f"Area: {r.area()}") # Output: Area: 50
print(f"Perimeter: {r.perimeter()}") # Output: Perimeter: 30
"""
Benefits of Abstract Classes:-
Enforcing Design: Ensures that derived classes implement specific methods, promoting consistency.
Polymorphism: Abstract classes enable polymorphic behavior where different subclasses implement the abstract methods differently.