Questions tagged [inheritance]
Inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support.
585 questions
0
votes
2
answers
335
views
C# - Entity handler correct use clean code
I have a question about setting up my service. My main concern is if the design is clean and in order. Whether it meets the SOLID principles and does not spill out on me.
I have entities like order ...
2
votes
3
answers
324
views
How do I model this scenario so that it adheres to OOP principles?
I have a Slide class with subclasses referring to the different types of slides (IntroSlide, SummarySlide, etc.):
abstract class Slide {
String slideType;
final String title;
final String ...
2
votes
3
answers
271
views
"use auto" and "declare most abstract type", which guideline has higher priority?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare most abstract type when possible, for example, suppose I'm using an UI ...
5
votes
2
answers
765
views
Is "using active record pattern" a reason to inherit from standard container (eg:vector)?
According to Is it bad practice to use Inheritance to associate methods with a basic container?, I know it is bad to inherit form std containers, mainly because std containers are not designed to be ...
5
votes
4
answers
2k
views
If class B extends A, can we say that B depends on A?
Let's say we have 2 (Java) classes:
class A {}
class B extends A {}
To me, saying B extends A and B is dependent on A are both true (but not equivalent) in this situation. My colleague, OTOH, says ...
2
votes
1
answer
398
views
Inheriting a logger
When you define a class, is inheriting a logger such as log4cxx a good design?
Assume I am defining a class called MyClass.
When I want a logger, I use a pointer to an abstract logger class as a ...
1
vote
4
answers
1k
views
What's the alternative to trying to inherit static methods?
I know you can't inherit static methods, and it seems the consensus is that if you feel like you need to, you're doing something wrong.
However, I don't know what the alternative is in my case. Please ...
0
votes
5
answers
2k
views
Comparing Java objects with different member variables
I have a base class "People" which two other classes inherit from: Employee and Student. The Student class includes a GPA (type double) and the Employee class does not. I have an ArrayList ...
0
votes
2
answers
404
views
Does Clean Code suggest avoiding all base class instance variables (include private instance variables)?
According to Why is Clean Code suggesting avoiding protected variables?, I should avoid protected variables because of the reason : "closely related concepts should not be separated into ...
2
votes
2
answers
157
views
What is the threshold of "usage" of a parent class member that should move to child class?
For example, for base and child classes, if all child class need a class member, eg: baseHp, which represents the base hp to calculate the actual hp of monsters in a game:
public class Monster{
...
15
votes
6
answers
6k
views
Why is it necessary to mark classes as not inherited from? Can't an optimizer automatically detect that virtual calls are unnecessary?
In C# and C++, an apparent trend is in place to reduce / avoid inheritance:
C#: "Sealing types can improve performance."
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/...
2
votes
2
answers
505
views
Composition over inheritance: how data are accessed in the composition case?
I've been reading this Wikipedia article Composition over inheritance. It gives a code example of inheritance first, and then a code example of composition. In case of inheritance there are data that ...
0
votes
0
answers
589
views
What is the Best Practice for handling multiple Entities that behave identically?
Because I have multiple entities with unique fields, I need multiple repositories for each one even though each Entity will be handled exactly the same. What is the best way to handle these separate ...
1
vote
1
answer
213
views
Is it an acceptable pattern to put derived classes inside an abstract base class in Java?
Suppose I have some Java code such as the following (in this case, the use of the name "interaction" is referring to interacting with an object in a video game):
public abstract class ...
-1
votes
3
answers
656
views
Is it better to override methods in classes or make methods general?
I am creating the backend of a microservice that will serve as a tool to see in real time how the company's employees are distributed by projects and what days they have assigned to each one. The ...
2
votes
2
answers
249
views
Explain forbidden inheritance across the domain dimensions of representation/implementation vs semantics
I am going through this nice write up on thoughtworks about inheritance vs composition. I understood the authors explanation of the dual purpose of inheritance. But, I am confused on the specific ...
21
votes
6
answers
7k
views
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
In Python 3, I subclassed int to forbid the creation of negative integers:
class PositiveInteger(int):
def __new__(cls, value):
if value <= 0:
raise ValueError("value ...
2
votes
4
answers
741
views
Large Inheritance Hierarchy vs. One Object With Many Nullable Fields
I am working on implementing some stock order types for a financial technology application. There are six different types of stock orders - market, limit, stop_loss, stop_loss_limit, trailing_stop, ...
0
votes
3
answers
1k
views
How to handle subclasses needing different method signatures for the overriden function?
I have an abstract class that represents chess pieces, it has an abstract method isMoveValid(Square futurePosition, PieceColor color) which checks if the piece moving to that square is valid or not, ...
2
votes
4
answers
435
views
Database design - how to model database tables for similar objects - booleans / enums / inheritance / archived vs active
I only recently started to work with backends. My database is supposed to store information about maven-like artifacts. There are going to be applications and configuration artifacts. Now those ...
50
votes
7
answers
11k
views
Why is inheritance bad in a Person-Student model?
I've just started learning about Inheritance vs Composition and it's kind of tricky for me to get my head around it for some reason.
I have these classes:
Person
class Person
{
public string Name {...
2
votes
3
answers
1k
views
Should classes with business logic inherit from a class with helper methods, or vise-versa?
I have a codebase where some classes contain both "essential" business logic, and "incidental" complexity. I am considering a refactor where I leverage inheritance to improve the ...
0
votes
2
answers
1k
views
How to concretize a return type when inheritance is used?
I have two repositories:
class RepositoryOne
{
/**
* @param int $id
* @return ModelOne
*/
public function getById($id)
{
// Search and find a ModelOne model in the ...
3
votes
2
answers
1k
views
Is inheritance constraint on Generics redundant? If not what is its recommended use?
I've recently found myself using a generics with constraint that a type should inherit from a specific base class, but now I've just realised that this is redundant and unnecessary because the ...
0
votes
1
answer
298
views
Wrapping the UI framework
I want to write an application where I would delegate certain functionalities to 3rd party libraries. To make sure the code remains modular, I want to put these libraries behind an interface so I can ...