0

I was reading about abstract base class and came across https://www.python-course.eu/python3_abstract_classes.php website. I got general idea about them but I found two statement contradictory of each other.

Subclasses of an abstract class in Python are not required to implement abstract methods of the parent class.

and

A class that is derived from an abstract class cannot be instantiated unless all of its abstract methods are overridden.

My understanding of first statement is, derived class are not required to implement abstract method of the parent class which is wrong. I made a sample program to check that.

from abc import ABC, abstractmethod

class AbstractClassExample(ABC):

    @abstractmethod
    def do_something(self):
        print("Some implementation!")

class AnotherSubclass(AbstractClassExample):

    def just_another_method(self):
        super().do_something()
        print("The enrichment from AnotherSubclass")

x = AnotherSubclass() # TypeError: Can't instantiate abstract class AnotherSubclass with abstract methods do_something
x.do_something()

I would like an explanation of what the first statement means(preferably with examples).

6
  • Your code demonstrates the first statement, doesn't it? If you delete x = AnotherSubclass(), then your code will run without error. Commented Dec 10, 2019 at 18:10
  • If I remove that then there is nothing running in the program, so there won't be any error. My point is for AnotherSubclass to be instantiated we must have to implement the do_something method, and I think first statement contradicts that Commented Dec 10, 2019 at 18:12
  • 1
    @Sweeper oh I think I got it now, we can make the class, but not instantiate it? Commented Dec 10, 2019 at 18:15
  • Exactly! That is what the first statement says. Note that this is more subtle than "the program does nothing so of course no error!" because if you, say, try to remove some empty spaces at the start of lines you'll get errors even if your program "does nothing". Commented Dec 10, 2019 at 18:15
  • 1
    Yep, your second comment got it. I will post an answer. Commented Dec 10, 2019 at 18:16

1 Answer 1

1

Your code demonstrates that the second statement is true. It doesn't show that the first statement is false.

In your code, you are trying to instantiate AnotherSubclass, which is not allowed because AnotherSubclass does not implement all the abstract methods. The second statement says this.

However, if you delete the last two lines, i.e. not instantiating AnotherSubclass, then your code will produce no errors when you try to run it. This shows that the first statement is true - subclasses of abstract classes that doesn't implement all its abstract methods are allowed to exist.

You can write another subclass of AnotherSubclass called YetAnotherClass, this time implementing the abstract method, and you will be able to instantiate YetAnotherClass. Note that your program now does something, and AnotherSubclass is still allowed to exist.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.