Questions tagged [abstract-methods]

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.

157 questions
57
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
  • 11,379
  • 15
  • 76
  • 122
54
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
53
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
  • 3,103
  • 2
  • 34
  • 37
35
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,227
  • 2
  • 16
  • 14
28
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,759
  • 3
  • 20
  • 25
21
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
  • 15,768
  • 29
  • 82
  • 105
21
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
  • 901
  • 2
  • 10
  • 37
18
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
  • 289
  • 2
  • 3
  • 7
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,019
  • 3
  • 16
  • 36
13
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,154
  • 10
  • 26
13
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,143
  • 4
  • 38
  • 54
12
votes
11 answers

"Abstract static" method - how?

There are already several SO questions on why there is not abstract static method/field as such, but I'm wondering about how one would go about implementing the following psuedo-code: class Animal { abstract static int getNumberOfLegs(); // not…
polyglot
  • 9,197
  • 7
  • 45
  • 62
11
votes
5 answers

Changing abstract method signatures in inherited classes

Imagine I have a class called Engine as an abstract base class. I also have ElectrictEngine and FuelEngine classes which derive from it. I want to create a method for refueling the engine. Should I do it as an abstract method on the base class level…
Mulder
  • 2,429
  • 2
  • 14
  • 7
11
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
  • 992
  • 6
  • 22
11
votes
2 answers

python abstractmethod with another baseclass breaks abstract functionality

Consider the following code example import abc class ABCtest(abc.ABC): @abc.abstractmethod def foo(self): raise RuntimeError("Abstract method was called, this should be impossible") class ABCtest_B(ABCtest): pass test =…
IARI
  • 946
  • 1
  • 11
  • 30
1
2 3
10 11