Module (72%)
Section (75%)

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:

  1. it’s possible to instantiate the GreenField class and call the hello method;
  2. the RedField class is still recognized as an abstract one, because it inherits all elements of its super class, which is abstract, and the RedField class does not override the abstract hello method.

Code

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()
{{ dockerServerErrorMsg }} ×
{{ errorMsg }} ×
{{ successMsg }} ×