Questions tagged [abstract-class]
An abstract class is a class that cannot be instantiated. They are generally meant to be extended/subclasses and generally have "abstract methods" that must be implement by subclasses.
127 questions
4
votes
2
answers
309
views
Interface + Trait vs Abstract Class
While developing my application I faced an interesting situation:
I have multiple DTO's which are almost identical in terms of methods, only properties are different. For example I have AccountDTO ...
1
vote
2
answers
214
views
Using an instance method or a function with conditional logic based on type
I'm developing a data pipeline in Python. We receive a variety of different types of data set that we need to process (e.g. xml, json, csv and some Excel workbooks) - where processing involves parsing ...
0
votes
1
answer
1k
views
Interfaces vs abstract classes for immutable DTOs
At my org we extensively use @Value.Immutable annotation to generate Immutable classes (usually with builders) for our internal data transfer objects (DTOs). In our codebase I've seen both interfaces ...
3
votes
3
answers
2k
views
C++ Is it okay to use nested classes as a way to namespace derived classes?
I have many abstract classes that describe many abstract ideas and objects. These classes have many complex relationships with each other, and I realize that while writing code with some of the ...
0
votes
0
answers
70
views
How to group together common allowed descendants of a vertex in a tree data data structure?
I created an abstract class to represent a vertex in a tree structure. Later, a requirement was introduced where certain types of vertices are not allowed as descendants of certain other vertices. So ...
2
votes
4
answers
2k
views
My concrete classes only have a constructor. Everything else comes from an abstract class. Is this bad practice?
I think that I've taken the Open-Closed and Single Responsibility principles too far. Previously, I had one huge static class containing every method that has C# talk to stored procedures on my SQL ...
0
votes
1
answer
1k
views
Abstract base classes and mix-ins in python
In the python docs, I read this about the ABC (abstract base class) meta class:
Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class.
I don't come ...
2
votes
2
answers
170
views
OOP Best practices: Is there any reason to separate out Factory functionality from an abstract base class?
Consider the following python3 code:
from abc import ABC, abstractmethod
class Food(ABC):
_food_factory_map = {}
_recipes = {}
@classmethod
def getFood(cls, foodName):
return ...
0
votes
3
answers
2k
views
Is it code smell to make an abstract child class override a parent method which only calls its own abstract method
Is it code smell to make an abstract child class implement a method, which overrides a parent method, whose only purpose is to call another abstract method? I want to make sure that anyone who ...
0
votes
0
answers
342
views
Unity ScriptableObject vs Abstract class for Spells
I am trying to create a spell casting system for an auto chess game.
I find myself not knowing which direction to go in my design. I started out with a Spell as a scriptable object that contains a ...
2
votes
3
answers
681
views
Object Oriented Programming - what is the best way to add new parameters?
I have a doubt about what would be the right OOP approach for implementing classes that do similar stuff with different parameters.
To provide a simple example, I would use two polynomial classes, ...
-1
votes
2
answers
194
views
Is an abstract role needed to ensure that a customer can only have one role out of several? [closed]
I have the following UML class diagram in a C++ context. I want to make sure that a Customer can have only one role:
Can the Customer now only assume one role, and what does <<abstract>> ...
2
votes
1
answer
142
views
Proxy Pattern in Python
I'm reading a book on design patterns. On proxy pattern the code are following:
class SensitiveInfo:
def __init__(self):
self.users = ['nick', 'tom', 'ben', 'mike']
def read(self):
...
1
vote
3
answers
720
views
Refactoring a class to an abstract base class - keep default implementation in place or move it to an an other class?
I have a class which aggregates some temporal data from a database and provides a bunch of methods to query said data. It looks like this:
public class InfoProvider
{
public IEnumerable<...
-2
votes
3
answers
1k
views
Singleton as Interface for testability via dependancy injection
It can be found in many advices on topic that having Singletons is an anti-pattern. Especially for cases of testability. Can someone please advice/critique on this way (please see code below) of ...
1
vote
1
answer
249
views
Abstract Base Class decides which type of child class to create at runtime
I have an Abstract Base Class AbstractModel
class AbstractModel {
public:
struct predictionStructure{};
virtual predictionStructure predict(CompanyLib::Matrix<double> data) = 0;
std::...
0
votes
1
answer
1k
views
How to expose C++ static library interface, extending Pimpl to an abstract interface
Until today I had a static C++ library with no separation between the public interface and internal headers. My other apps just linked to it, included the required headers, and used whatever they ...
4
votes
4
answers
1k
views
How name public method that relays to abstract methods of its children (c#)
I've run into the following situation multiple times, and in every case have struggled with the naming.
I want a class to force its children to implement a method, but I want the parent class to be ...
0
votes
3
answers
295
views
Java Inheritance Problem
I have the finance application where we can have different types of transactions, like balance enquiry, funds transfer, and pin change etc. Now the Transaction is a base class, and there are specific ...
2
votes
2
answers
2k
views
Is there a difference between abstract data type and interface?
From what I understand, an abstract data type is basically some data and what we are allowed to do with that data (ex. a list with a set of data and an attribute size and the functions get(), set(), ...
9
votes
4
answers
3k
views
OOP Design considering no modifications to existing design
The question is:
interface Animal {
void eat();
}
class Lion implements Animal{
public void eat(){
//do somethng
}
}
class Test {
public static void main(String[] args) {
...
3
votes
1
answer
619
views
Design Patterns: Factory Pattern Vs. getInstance Inside Abstract Class
I'm working on an app where we need to use different authentication flows depending on how the user is accessing the app. I want to make sure that there is only one instance of the authentication ...
1
vote
1
answer
127
views
Can this simple Bank example be considered as a valid Abstract Factory?
For teaching purposes, I'm trying to replicate in a more faithful way from this conceptual UML (from wikipedia):
In a "so-so" real world example, in my case, families of Loans and Insurances:
So, can ...
0
votes
1
answer
523
views
What is the difference between "Abstract Core" and "Pluggable Component Framework" concepts
I am reading Domain Driven Design by Eric Evans and there are 2 sub-chapters - Abstract Core & Pluggable Component Framework - that seems to me to refer to the same concept. I believe that there ...
1
vote
5
answers
8k
views
When should i use an abstract class vs an interface? [duplicate]
I'm fairly new to programming. At school I am currently learning to program with Java. I want to build an application where i can store my collection of books, records, boardgames and such. Started ...
24
votes
4
answers
7k
views
Never make public members virtual/abstract - really?
Back in the 2000s a colleague of mine told me that it is an anti-pattern to make public methods virtual or abstract.
For example, he considered a class like this not well designed:
public abstract ...
5
votes
1
answer
2k
views
How to go ahead with methods that only one derived class implements from a common interface?
I have one interface (let's say in C++) that has been implemented by some derived classes. Let's suppose that the interface is like this:
class IBase
{
virtual bool method_1() = 0;
virtual long ...
2
votes
1
answer
271
views
Using virtual inheritance for an interface system based on abstract classes
I want to use a physics engine (like bullet or PhysX) in my program, however I want to hide the actual physics engine from it, so I can easily swap it out with another during run-time (e.g. switch ...
3
votes
1
answer
4k
views
Should an abstract class implement an interface, as opposed to defining its own abstract methods?
I'm defining a class structure for persisting to our cassandra database, and I'm unsure about using a combination of an abstract class and an interface. I have two concrete classes, one for persisting ...
3
votes
1
answer
11k
views
Unit testing abstract classes with Google mock (gmock/gtest) (C++)
I want to test an abstract class with:
Pure virtual methods that should be overridden in sub-classes
Non-pure virtual methods that use the pure virtual methods (as opposed to this question)
class Fu
{
...
4
votes
2
answers
117
views
How are settings structured when they can be configured in diffferent ways?
Suppose of this question the following:
I'm in full control of this project
I'm writing a media player
Obviously, a media player will allow a user to adjust the volume, so I might have a class that ...
17
votes
4
answers
4k
views
When to move a common field into a base class?
I currently have two derived classes, A and B, that both have a field in common and I'm trying to determine if it should go up into the base class.
It is never referenced from the base class, and say ...
1
vote
1
answer
103
views
Pseudo-Factory Abstract Class
So I have a requirement for something like this:
The client shouldn't be aware of how the actual classes are
implemented or constructed.
The classes implement a common interface
So I used Factory ...
3
votes
5
answers
1k
views
How to avoid the continuous downcasting in this case?
I have an abstract class Dog and multiple subclasses (Beagle, Labrador, Bulldog...) extendig it. I have a DogHouse that can store a Dog.
My problem is that when I put for example a Beagle into a ...
1
vote
3
answers
5k
views
Dependency Injection via Constructors vs Abstract Classes
For the past few days I've been researching the relationship of abstract classes and dependency-injected via the constructor classes.
It appears that any time that I can have a dependency-injected ...
1
vote
3
answers
706
views
Should a property be in an abstract class if not directly used
I have an abstract base class that is inherited by several different other types. They were all using a type injected into their constructor. So I moved this property in to the abstract base class. ...
13
votes
5
answers
6k
views
Does it make sense to define an interface if I already have an abstract class?
I have a class with some default/shared functionality. I use abstract class for it:
public interface ITypeNameMapper
{
string Map(TypeDefinition typeDefinition);
}
public abstract class ...
32
votes
2
answers
6k
views
Implementation of pure abstract classes and interfaces
Although this isn't mandatory in the C++ standard, it seems the way GCC for example, implements parent classes, including pure abstract ones, is by including a pointer to the v-table for that abstract ...
2
votes
1
answer
113
views
Feedback on inheritance assignment
So I've got this java assignment for college and was just wondering if anyone here could give me some feedback as to what I've been thinking some of this means.
I've got a .csv file with building ...
21
votes
4
answers
4k
views
Use abstract class in C# as definition
As a C++ developer I'm quite used to C++ header files, and find it beneficial to have some kind of forced "documentation" inside the code. I usually have a bad time when I have to read some C# code ...
0
votes
3
answers
410
views
Send records using async or sync way
I have bunch of keys and values that I want to send to our messaging queue by packing them in one byte array. I will make one byte array of all the keys and values which should always be less than 50K ...
2
votes
2
answers
976
views
Is there a best practice to forward method overrides while enforcing method execution?
Sometimes i have code along the lines of this:
public abstract class A
{
protected abstract void DoSomething();
}
public abstract class B : A
{
/// <...
0
votes
1
answer
541
views
How to design inheritance from abstract class which is not initiated but seemingly must be initiated?
I need to design a program in Java which calculates arithmetic expressions (only addition or subtraction).
Requirements:
1) abstract class Expression which contains abstract method calculate()...
3
votes
1
answer
534
views
Best design for classes that draw objects but do not inherit from JPanel
I'm doing the exercise 10.1, page 476 from the book Java: How To Program, Early Objects by Paul and Harvey Deitel (10th Edition).
Modify the MyLine, MyOval and MyRectangle classes of GUI to create ...
2
votes
2
answers
7k
views
Sequence diagram for Abstract and Derived class
I've a bunch of classes where one is Abstract class. I draw few derived class from that Abstract base class. For example,
class IBase{
public:
*register(): bool*
*update(): bool*
};
class Derived:...
11
votes
6
answers
7k
views
What code should be included in an abstract class?
I am troubled lately about the use of abstract classes.
Sometimes an abstract class is created in advance and work as a template of how the derived classes would work. That means, more or less, that ...
2
votes
1
answer
2k
views
How to avoid the static_cast/dynamic_cast in `Abstract Factory` design pattern?
We are using Abstract Factory design pattern in our project, as the project became complex, most of the time the concrete class functionality need to separate to multiple class.
As the following code ...
9
votes
3
answers
6k
views
Properly designing an abstract class: How to retrieve data?
I come from a self taught background and am trying to shore up my weaknesses in OOP, specifically C# and class design. I've been reading Code Complete 2 and it became apparent that I'm not following ...
5
votes
1
answer
449
views
When can client code know about strategy pattern implementations?
I am considering using a strategy pattern for configuration file management, that way I can support some legacy configs. I feel pretty solid on the overall design (as its pretty standard strategy ...
1
vote
2
answers
642
views
Options for derived classes of two abstract base classes
Let's say there are classes D1, D2, etc. describing different types of an abstract class D.
Let's say there are SenderReceiver classes describing different ways of communicating for each D1, D2, etc.:...