-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstractclass.py
More file actions
40 lines (31 loc) · 1.02 KB
/
abstractclass.py
File metadata and controls
40 lines (31 loc) · 1.02 KB
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
# class Shape:
# def area(self): pass
# def perimeter(self): pass
#
#
# class Square(Shape):
# def __init__(self, side):
# self.__side = side
#
#
# shape = Shape()
# Suppose we don't want that class shape couldn't be called as is done in this line.
# for this abstract class function is used
# Abstract class is method to restrict classes not to be called.
# in python there is no explicit abstract class but there is built-in module for that
from abc import ABC, abstractmethod # Abstract based Classes
class Shape(ABC):
@abstractmethod # abstractmethod is decorator method which must be implemented in subclass as abstract
def area(self): pass
@abstractmethod
def perimeter(self): pass # perimeter and area method are abstract methods
class Square(Shape):
def __init__(self, side):
self.__side = side
def area(self):
return self.__side * self.__side
def perimeter(self):
return self.__side * 4
square = Square(5)
print(square.perimeter())
print(square.area())