In OOP (Object Oriented Programming), a base class that cannot be instantiated because some of its declared methods lack a definition, which is intended to be provided by derived classes. The term may have more specific or slightly different meaning according to the particular computer language involved.
Questions tagged [abstract-base-class]
109 questions
49
votes
3 answers
Can a Python Abstract Base Class enforce function signatures?
Suppose I define an abstract base class like this:
from abc import abstractmethod, ABCMeta
class Quacker(object):
__metaclass__ = ABCMeta
@abstractmethod
def quack(self):
return "Quack!"
This ensures any class deriving from Quacker must…

simonwo
- 3,171
- 2
- 19
- 28
24
votes
6 answers
Abstract vs. Interface - separating definition and implemention in Delphi
What is the better approach for separating definition and implementation, using interfaces or abstract classes?
I actually I don't like mixing reference counted objects with other objects. I imagine that this can become a nightmare when maintaining…

markus_ja
- 2,931
- 2
- 28
- 36
21
votes
1 answer
Using abstract base class VS plain inheritance
I'm trying to understand the benefits of using abstract base classes. Consider these two pieces of code:
Abstract base class:
from abc import ABCMeta, abstractmethod, abstractproperty
class CanFly:
__metaclass__ = ABCMeta
@abstractmethod
…

Derek Chiang
- 3,330
- 6
- 27
- 34
16
votes
3 answers
How to get an abstract dataclass to pass mypy?
mypy v0.910 rejects abstract dataclasses in Python 3.9. Here's the Minimal Reproducible Example:
from abc import ABC, abstractmethod
from dataclasses import dataclass
@dataclass
class Liquid(ABC):
@abstractmethod
def drip(self) -> None:
…

Ben Kovitz
- 4,920
- 1
- 22
- 50
16
votes
3 answers
What does isinstance with a dictionary and abc.Mapping from collections doing?
The code I'm running is:
>>> from collections import abc
>>> mydict = {'test_key': 'test_value'}
>>> isinstance(mydict, abc.Mapping)
True
I understand what isinstance does, but I'm not sure what abc.Mapping does from collections?
It seems like the…

Vincent
- 7,808
- 13
- 49
- 63
15
votes
1 answer
python error when initializing a class derived from and abstract one
I have this simple code and I get a strange error:
from abc import ABCMeta, abstractmethod
class CVIterator(ABCMeta):
def __init__(self):
self.n = None # the value of n is obtained in the fit method
return
class…

Donbeo
- 17,067
- 37
- 114
- 188
12
votes
3 answers
Which Exception for notifying that subclass should implement a method?
Suppose I want to create an abstract class in Python with some methods to be implemented by subclasses, for example:
class Base():
def f(self):
print "Hello."
self.g()
print "Bye!"
class A(Base):
def g(self):
…

Manuel Araoz
- 15,962
- 24
- 71
- 95
12
votes
8 answers
Abstract Base Class vs. Concrete Class as a SuperType
After reading the most excellent book "Head First Design Patterns", I began proselytizing to my colleagues the benefits of patterns and design principles. While extolling the virtues of my favorite pattern - Strategy Pattern - I was asked a…

Tony D
- 1,531
- 17
- 29
12
votes
1 answer
Catching exceptions based on their abstract base class
Suppose I've got an exception class with an abstract base class, something like this:
class MyExceptions(BaseExeption, metaclass=abc.ABCMeta):
pass
class ProperSubclass(MyExceptions):
pass
MyExceptions.register(ValueError)
It appears that…

abingham
- 1,294
- 1
- 10
- 17
11
votes
3 answers
C++ Calling a copy constructor on an unknown derived class through an abstract base class
I'm making a tree that has several different node types: a binary node, a unary node, and a terminal node. I've got an ABC that all the nodes inherit from. I'm trying to write a recursive copy constructor for the tree like so:
class…

LinuxMercedes
- 251
- 5
- 10
10
votes
1 answer
typing.Protocol class `__init__` method not called during explicit subtype construction
Python's PEP 544 introduces typing.Protocol for structural subtyping, a.k.a. "static duck typing".
In this PEP's section on Merging and extending protocols, it is stated that
The general philosophy is that protocols are mostly like regular…

Jasha
- 5,507
- 2
- 33
- 44
10
votes
2 answers
Able to instantiate python class, in spite of it being Abstract (using abc)
This is in reference to the answer for this question to "Use abc module of python to create abstract classes." (by @alexvassel and accepted as an answer).
I tried the suggestions, but strangely enough, in spite of following the suggestions to use…

jark
- 305
- 1
- 3
- 11
7
votes
2 answers
How to create base class for all my symfony controllers
I want to create a base controller class for all my controllers in Symfony, I am very new to Symfony, so don't be angry with dumb question. I am asking this question because I can't do something like this
use…

Aderemi Dayo
- 705
- 1
- 11
- 25
7
votes
1 answer
Python Abstract Base Classes: Why doesn't abc prevent instantiation?
As far as I understood, the Python module abc should prevent instantiation of classes which have not all @abstractmethod marked methods of the base class implemented (provided that the base class has __metaclass__ = ABCMeta set)
However, this seems…

Andreas Duering
- 1,560
- 2
- 12
- 21
7
votes
2 answers
Scala: Trait Mixin with Abstract Base Class
I have an abstract base class (Base) that has some stacking traits defined for it (StackingTrait).
trait Base {
def foo
}
trait StackingTrait extends Base {
abstract override def foo { super.foo }
}
It would be very convenient to implement a…

sient
- 600
- 1
- 5
- 11