Questions tagged [abstract-class]

Abstract classes are classes which cannot be instantiated. They exist to provide common functionality and interface specifications to several concrete classes.

Abstract classes are classes which cannot be instantiated. They exist to provide common functionality and interface specifications to several concrete classes.

From Abstract Type on Wikipedia:

Abstract classes can be created, signified, or simulated in several ways:

  • By use of the explicit keyword abstract in the class definition, as in Java, D or C#.
  • By including, in the class definition, one or more methods (called pure virtual functions in C++), which the class is declared to accept as part of its protocol, but for which no implementation is provided.
  • By inheriting from an abstract type, and not overriding all missing features necessary to complete the class definition.
  • In many dynamically typed languages such as Smalltalk, any class which sends a particular method to this, but doesn't implement that method, can be considered abstract. (However, in many such languages, the error is not detected until the class is used, and the message returns results in an exception error message such as "does Not Understand").
5262 questions
2
votes
1 answer

How to call non abstract method in a abstract class?

I have an abstract class in python and want to call non-abstract methods in it. Is it possible to do it? from abc import ABC, abstractmethod class MyAbstract(ABC): # Can I call method get_state() from get_current() ? def get_state(): …
Laura Smith
  • 293
  • 3
  • 13
2
votes
0 answers

Testing an Abstract Class with an HttpClient Dependence in Angular

I am trying to test an abstract service class that uses HttpClient. To do this I've followed the suggestion in this thread by implementing the most basic instantiation of the abstract class, and trying to test that. However I still can't get it to…
bicarlsen
  • 1,241
  • 10
  • 27
2
votes
2 answers

Python NotImplementedError for instance attributes

How do I mark an instance attribute as not implemented in a base class? (Distinct from this question which discusses marking class attributes as not implemented, but perhaps I'm not understanding base classes correctly...) e.g. I want something…
dkv
  • 6,602
  • 10
  • 34
  • 54
2
votes
1 answer

How can I overload an operator for an abstract class?

I'm relatively new to C++, and my professor did not go into as much detail into operator overloading as I'd like when I took his class. I'm trying to implement a way to compare objects (using > or <) that all inherit an abstract class, but I'm…
GWF
  • 23
  • 2
2
votes
0 answers

Require child of abstract class to not override method

With python abstract classes, is there a way to require that a class instance does not override a particular method? Using the @abstractmethod decorator on a method requires the child class to define the method. Skipping the @abstractmethod…
zkurtz
  • 3,230
  • 7
  • 28
  • 64
2
votes
0 answers

Mapping abstract class by ModelMapper

I have such a simple class structure which has abstract classes and subclasses. I want them to be mapped to destination class structure using ModelMapper. I have checked and none of the solutions in other questions related to mapping abstract…
zolv
  • 1,720
  • 2
  • 19
  • 36
2
votes
1 answer

Should I initialize members of an abstract class?

I'd like to know if it is best practice to initialize members of an abstract class (has at least one pure virtual member-function, possibly the dtor). The other choice is to do it in the inheriting classes. Initializing in inheriting class class…
SpectreVert
  • 187
  • 2
  • 10
2
votes
0 answers

How to constrain the type of the output of a method of an abstract class?

Say I have an abstract class: class A(metaclass=ABCMeta): def __init__(self): pass @abstractmethod def a_function(self) -> int: return 0 I want to constrain the type of the output of a_function so that when a class…
dallonsi
  • 1,299
  • 1
  • 8
  • 29
2
votes
1 answer

How to cast an variable with type abstract class to its subclass?

I'm writing an interface for a method. void method(Node* node); The interface has the code class Node { public: virtual void init(Node* a) = 0; }; The sub class has the code class CNode: public Node { public: void init(Node* a); …
vize_xpft
  • 67
  • 5
2
votes
2 answers

Can I enforce defining a specific constructor with an interface class?

I want to create an interface - an abstract class, which among others enforces derived classes (i.e. conforming to the interface) to provide a specific constructor. Something along the lines: class IComClient { public: virtual IComClient(int…
SF.
  • 13,549
  • 14
  • 71
  • 107
2
votes
2 answers

How to add values to list>

I need to add elements of derived classes into list of shared pointers to abstract class. I've been trying this. I know that I am trying to create instance of abstract class in the example below, but I have no idea how to make it work. Simplified…
2
votes
5 answers

Using interface in C++ for dependency injection

Assume I have the following abstract class and use it as an "interface" in C++: class IDemo { public: virtual ~IDemo() {} virtual void Start() = 0; }; class MyDemo : IDemo { public: virtual void start() { //do stuff …
Icerman
  • 1,099
  • 4
  • 14
  • 30
2
votes
1 answer

How to realise generic abstract class?

I have abstract class: abstract class AScore { constructor( protected data: T) {} } I implement this class like: class GetActivitiesPupil implements AScore {} Compiler says it is wrong implementation of class
POV
  • 11,293
  • 34
  • 107
  • 201
2
votes
3 answers

How to execute a method in base class after child is fully initialized?

I implemented the following structure: public abstract class A { protected A() { Test(); } private void Test() { Console.WriteLine("2"); } } public class B : A { public B() : base() { …
Nico Schreiner
  • 397
  • 5
  • 13
2
votes
0 answers

How to inherit error handling in a Python class

I'm writing some boilerplate code, which has some abstract classes from which others will inherit. Not every method needs to run, so the abstract class handles exceptions, using @handleError as a decorator. I'll use dogs to illustrate (the actual…
sapo_cosmico
  • 6,274
  • 12
  • 45
  • 58