-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstract-Telusko.py
More file actions
94 lines (75 loc) · 2.54 KB
/
Copy pathAbstract-Telusko.py
File metadata and controls
94 lines (75 loc) · 2.54 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
"""
20200423---Abstract by Telusko
-------------------------------
Here you will have an Error (as below) if you tried with Abstract class only.
because abstract class can not be instantiate with it's abstract methods because
python doesn't have abstract class method as default itself. so, To overcome
with this we should make a child or sub class to instantiate
TypeError: Can't instantiate abstract class with abstract methods.
Ex:
from abc import ABC, abstractmethod
class Computer(ABC):
@abstractmethod
def process(self):
pass
com = Computer()
print(com.process())
"""
#another way-1
"""
# here also the same error you will get if you do the child class as declaration
# because Lap class instantiated the Computer Abstract class method but method
# is not defined in child class. so while instantiating the abstract class from
# child class then method should be defined then only abstract class will work.
from abc import ABC, abstractmethod
class Computer(ABC):
@abstractmethod
def process(self):
pass
class Lap(Computer):
pass
#com = Computer()
com1 = Lap()
#print(com1.process())
"""
#child class-1
"""
from abc import ABC, abstractmethod
class Computer(ABC):
@abstractmethod
def process(self):
pass
class Lap(Computer):
def process(self):
print("SUccess...Abstract class instantiated from Lap child class with defined method.")
#com = Computer() #you can not call the abstract class to an object because method
#is not defined and to define the method you should have sub class
com1 = Lap()
#com1.process()
"""
#adding more child classes
from abc import ABC, abstractmethod
class Computer(ABC):
@abstractmethod
def process(self):
pass
class Lap(Computer):
def process(self):
print("Abstract class (Computer) instantiated by Lap child class.")
class programmer:
def work(self,prog):
print("programmer class instantiated the Lap class")
prog.process()
class Developer:
def dev(self,devlap):
print("Developer class instantiated the Lap class method and programmer method")
#devprog.work()
devlap.process()
#com = Computer() #you can not call the abstract class to an object because method
#is not defined and to define the method you should have sub class
com = Lap()
com.process()
com1 = programmer()
com1.work(com) #here i should pass the object of first child class (Laptop)
com2 = Developer()
com2.dev(com)