Is it possible to have an Abstract Class inheriting from another Abstract Class in Python?
If so, how should I do this?
A class that is derived from an abstract class cannot be instantiated unless all of its abstract methods are overridden.
In ABSTRACT class,we can't extends multiple abstract classes at a time. but In INTERFACE, we can implements multiple interfaces at time. Therefore , interfaces are used to achieve multiple inheritance in java.
A class can inherit from multiple abstract classes.
Note the abstract base class may have more than one abstract methods. The child class must implement all of them failing which TypeError will be raised.
Have a look at abc
module. For 2.7: link. For 3.6: link
Simple example for you:
from abc import ABC, abstractmethod
class A(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def do_something(self):
pass
class B(A):
@abstractmethod
def do_something_else(self):
pass
class C(B):
def do_something(self):
pass
def do_something_else(self):
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With