Now we'll try to inherit the abstract class and forget about overriding the abstract method by creating a RedField class that does not override the hello method.
import abc
class BluePrint(abc.ABC):
@abc.abstractmethod
def hello(self):
pass
class GreenField(BluePrint):
def hello(self):
print('Welcome to Green Field!')
class RedField(BluePrint):
def yellow(self):
pass
gf = GreenField()
gf.hello()
rf = RedField()
The output:
Welcome to Green Field!
Traceback (most recent call last):
(...)
rf = RedField()
TypeError: Can't instantiate abstract class RedField with abstract methods hello
output
indicates that:
- it’s possible to instantiate the
GreenFieldclass and call thehellomethod; - the
RedFieldclass is still recognized as an abstract one, because it inherits all elements of its super class, which is abstract, and theRedFieldclass does not override the abstracthellomethod.
Code
import abcclass BluePrint(abc.ABC):
@abc.abstractmethod
def hello(self):
pass
class GreenField(BluePrint):
def hello(self):
print('Welcome to Green Field!')
class RedField(BluePrint):
def yellow(self):
pass
gf = GreenField()
gf.hello()
rf = RedField()
{{ dockerServerErrorMsg }}
×
{{ errorMsg }}
×
{{ successMsg }}
×