-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkt_11_oops_05_abstraction.py
More file actions
104 lines (76 loc) · 2.06 KB
/
kt_11_oops_05_abstraction.py
File metadata and controls
104 lines (76 loc) · 2.06 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 1. Python - Abstraction
"""
Abstract Method Overriding
Moreover, the class must have at least one abstract method.
"""
from abc import ABC, abstractmethod
class Democlass(ABC):
@abstractmethod
def method1(self):
print("abstract method")
return
def method2(self):
print("concrete method")
class Concreteclass(Democlass):
def method1(self):
print("Concreteclass==>method1 before ")
super().method1()
print("Concreteclass==>method1 after ")
return
# def muthu(self):
# print("Concreteclass==>method1 before ")
# super().method1()
# print("Concreteclass==>method1 after ")
# return
obj = Concreteclass()
obj.method1()
# obj.muthu()
obj.method2()
"""
Formal Interface
Informal Interface
"""
# 1. Formal Interface
"""
Formal interfaces in Python are implemented using abstract base class (ABC). To use this class,
you need to import it from the abc module.
"""
from abc import ABC, abstractmethod
# creating interface
class DemoInterface(ABC):
@abstractmethod
def method1(self):
print("Abstract method1")
return
@abstractmethod
def method2(self):
print("Abstract method1")
return
# class implementing the above interface
class concreteclass(DemoInterface):
def method1(self):
print("This is method1")
return
def method2(self):
print("This is method2")
return
# creating instance
obj = concreteclass()
# method call
obj.method1()
obj.method2()
# 2. Informal Interface
"""
In Python, the informal interface refers to a class with methods that can be overridden. However,
the compiler cannot strictly enforce the implementation of all the provided methods.
"""
class DemoInterface:
def displayMsg(self):
pass
class NewClass(DemoInterface):
def displayMsg(self):
print("This is my message")
# creating instance
obj = NewClass()
# method call
obj.displayMsg()