An abstract method is one with only a signature and no implementation body. It is often used to specify that a subclass must provide an implementation of the method.
Questions tagged [abstract-methods]
186 questions
245
votes
7 answers
How to create abstract properties in python abstract classes?
In the following code, I create a base abstract class Base. I want all the classes that inherit from Base to provide the name property, so I made this property an @abstractmethod.
Then I created a subclass of Base, called Base_1, which is meant to…

Boris Gorelik
- 29,945
- 39
- 128
- 170
180
votes
5 answers
`staticmethod` and `abc.abstractmethod`: Will it blend?
In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod. How do I do this?
I tried applying both decorators, but it doesn't work. If I do this:
import abc
class C(object):
__metaclass__ = abc.ABCMeta
…

Ram Rachum
- 84,019
- 84
- 236
- 374
100
votes
2 answers
python @abstractmethod decorator
I have read python docs about abstract base classes:
From here:
abc.abstractmethod(function)
A decorator indicating abstract methods.
Using this decorator requires that the class’s metaclass is ABCMeta or
is derived from it. A class that has a…

Sebastian
- 4,770
- 4
- 42
- 43
67
votes
5 answers
When and Why to use abstract classes/methods?
I have some basic questions about abstract classes/methods. I know the basic use of abstract classes is to create templates for future classes. But are there any more uses for them? When should you prefer them over interfaces and when not? Also,…

Vishal
- 12,133
- 17
- 82
- 128
60
votes
4 answers
Can't instantiate abstract class with abstract methods
I'm working on a kind of lib, and I'm getting an error.
Here is my code. Of course @abc.abstractmethod have to be uncommented
Here are my tests
Sorry couldn't just copy and paste it
I went on the basis that the code below works.
test.py:
import…

josuebrunel
- 1,071
- 1
- 10
- 19
58
votes
13 answers
Is there a way to make a method which is not abstract but must be overridden?
Is there any way of forcing child classes to override a non-abstract method of super class?
I need to be able to create instances of parent class, but if a class extends this class, it must give its own definition of some methods.
user517491
49
votes
1 answer
What does a function without body mean?
I'm reading the code that package time, and then I want to know how the func After(d Duration) <-chan Time works.
I found the code follows:
func After(d Duration) <-chan Time {
return NewTimer(d).C
}
func NewTimer(d Duration) *Timer {
c…

YulCheney
- 2,877
- 2
- 18
- 14
33
votes
5 answers
Default implementation or abstract method?
Is it better to put a default implementation of a method in a superclass, and override it when subclasses want to deviate from this, or should you just leave the superclass method abstract, and have the normal implementation repeated across many…

Scott
- 1,869
- 3
- 20
- 25
23
votes
5 answers
Should an abstract class have at least one abstract method?
Is it necessary for an abstract class to have at least one abstract method?

java_geek
- 17,585
- 30
- 91
- 113
23
votes
9 answers
Why can abstract methods only be declared in abstract classes?
I understand that in abstract classes methods be both abstract, or not. But why can I not create an abstract method in a "normal", non-abstract class?
Thanks in advance for any explanation!

Jona
- 1,023
- 2
- 15
- 39
19
votes
1 answer
Body of abstract method in Python 3.5
I have an abstract class, Model, with a few abstract methods, what should I put in the body of the methods?
A return
class Model(metaclass=ABCMeta):
@abstractmethod
def foo(self):
return
A pass
class Model(metaclass=ABCMeta):
…

shayaan
- 1,482
- 1
- 15
- 32
19
votes
3 answers
Why do we need abstract classes in Java?
Why do we need abstract classes in Java? If you're never going to make it into an object, why have it in the first place? How do you use it? Why is it there?
I'm wondering the same thing with abstract methods. I find it seems like a similar concept…

user3314801
- 417
- 2
- 4
- 7
15
votes
1 answer
python - abstract method in normal class
I was reading official python documentation.
In the mentioned link, the second line states that:
Using this decorator requires that the class’s metaclass is ABCMeta or
is derived from it.
But, I was successfully able to define the below given…

harmands
- 1,082
- 9
- 24
15
votes
5 answers
C# design: Why is new/override required on abstract methods but not on virtual methods?
Why is new/override required on abstract methods but not on virtual methods?
Sample 1:
abstract class ShapesClass
{
abstract public int Area(); // abstract!
}
class Square : ShapesClass
{
int x, y;
public int Area() // Error: missing…

driAn
- 3,245
- 4
- 41
- 57
14
votes
3 answers
Best example of Abstract class in Android
I am trying to design one Abstract class and method in Android and call those methods by extending the class from my parent Activity class but I don't how to call my abstract method.
MyCode :
MainActivity.java
public class MainActivity extends…

deeptimancode
- 1,139
- 4
- 18
- 40