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.

Filter by
Sorted by
Tagged with
4
votes
1answer
164 views

Delegate vs Forwarding in Java OOP

I'm reading some article about "prefer composition over inheritance", and heard about Forwarding and Delegation. After search for the different I found some source: https://en.wikipedia.org/wiki/...
3
votes
2answers
153 views

When covariance becomes an issue, how can I restructure my code and still be clean?

Suppose I have the following interfaces, GameObject and Enhance. GameObject: public interface GameObject { void prepare(); void use(); void cleanup(); } Enhance: public interface ...
5
votes
3answers
486 views

Is it anti-pattern to have inheritence in a dto?

Are data transfer objects or POJOs meant to be final or can they be extended and create hierarchies for them? It is not clear to me if such a value class is properly designed only as a final class and ...
2
votes
6answers
498 views

What SOLID principles am I breaking with this code?

With the below pseudo code? Am I breaking any SOLID principles? interface i_pet string get_name() string get_species() color get_fur_color() end interface interface i_cat implements i_pet end ...
0
votes
1answer
132 views

Object oriented parsing: Is there a pattern or is my approach wrong?

The problem that I am chewing on comes from parsing, i.e. constructing objects in a sequential manner. The grammar is not prefix free, that is, there are more than one syntactical elements sharing the ...
1
vote
1answer
65 views

Avoiding “instanceof” and explicit casts when selecting applicable handlers for inheriting object

In my current project, I am trying to implement an environment to perform simulations of different workflows in a range of programs, websites, and mobile applications. These simulation subjects can ...
2
votes
1answer
104 views

Is making your ORM DAO a subclass of your Domain Model object a viable strategy?

I am trying to separate my domain model and associated business logic from my persistence model, particularly such that the domain model has no dependency on the ORM. One apparent way to do that was ...
-3
votes
1answer
287 views

Why do people keep reusing superclass names in their subclasses?

In my project, I found one of the project's classes reusing the same name as an official one. For example: from django.db import models class Model(models.Model): class Meta: abstract =...
5
votes
1answer
742 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
1answer
69 views

DDD: Type hierarchy of entities' property

Say I have an Delivery class which should have a Destination property (among many others). Destination can be an Address (country, city, address line, post code) or a Port (international code, name). ...
1
vote
1answer
214 views

Best pattern to solve problem where objects only differ in one attribute

As I am maintaining and extend a software system in Java, I saw a colleague (who left due to retirement) implementing a table with a generic approach. This approach is unluckily bound to tables (ui-...
-3
votes
1answer
159 views

Why constructor inheritance is not supported in Python? [closed]

Consider the case where I have the following classes, class A: def __init__(self): print("Class A") class B(A): def __init__(self): print("Class B") b = B() The expected ...
0
votes
1answer
108 views

How can I use disparate concrete classes that share similar functionality to create an interface that abstracts those similarities?

I've run into this issue a few times. I have preexisting framework objects that do not share a common ancestor. An example of this would be a class representing a an environment variable and a class ...
-2
votes
3answers
301 views

Does Java Have True Single Inheritance? [closed]

I've been doing some studying on the types of inheritance. From what I've learned: Single inheritance refers to when a class inherits another class. Multi-level inheritance refers to when a class ...
9
votes
9answers
713 views

Inheritance vs Composition For Chess Pieces

A quick search of this stackexchange shows that in general composition is generally considered more flexible than inheritance but as always it depends on the project etc and there are times when ...
0
votes
0answers
34 views

How to store metadata (e.g. table name) for a model class?

What is the proper way to get a metadata of the type in languages that doesn't allow static methods/functions to be overridden? For example, I'm using Dart: abstract class Model { // Constructor ...
2
votes
3answers
183 views

How to use a child class property in a function written for base class

in C#, suppose I have written this function public void TryToTurnOn( Device d ) { // some common logic // some more common logic } I then have a class that inherit Device that is ...
-1
votes
1answer
139 views

How to name base classes so that it's most convenient for those extending a framework? [closed]

I'm designing a game engine that is supposed to be overridden. I have, for example, a class called Character. Should I prefix this with BaseCharacter or should I expect that whoever uses the framework ...
0
votes
2answers
242 views

Is Pull Up Method really a good idea?

My intuition tells me that moving duplicate/common codes among subclasses to the superclass (so superclass now have a new method) is a bad idea, but I have seen this technique used/described in both ...
0
votes
1answer
82 views

Use-Cases for Two Inherited Actors

We have been developing a requirements model for a travel agency system. There are two kinds of member users: premium and free. Both users are able to cancel their previous reservations. Unlike ...
0
votes
1answer
72 views

Inheritance or composition for a more advanced implementation of a class?

So i have a class called VirtualMouse, it is used to perform mouse actions such as moving and clicking. public class VirtualMouse { public VirtualMouse() { } public void ...
1
vote
4answers
198 views

Object Oriented Design: Repositories and Inheritance

I have an OO design concern. Let's say my models as following: class Account {} class LocalAccount extends Account {} class SocialAccount extends Account {} These Account entities are persisted and ...
1
vote
1answer
65 views

ES6 - Where is it better to initialize a member - parent class or derived class?

I am trying to figure out if there is a better practice for initializing class members of derived classes in ES6 - in the child or the parent, and why? For example: Option 1: class AbstractAnimal {...
1
vote
3answers
158 views

Should we inherit from base class and implement interface in this scenarios?

I am new to object oriented design and learning about interfaces and design patterns. In this example, I am trying to create class for cars. My question: Is it good practice to use base class and ...
10
votes
5answers
1k views

How to implement RealNumber and ComplexNumber inheritance?

Hopefully not too academic... Let's say I need real and complex numbers in my SW library. Based on is-a (or here) relationship, real number is a complex number, where b in imaginary part of complex ...
16
votes
4answers
2k 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 ...
12
votes
2answers
897 views

Why does the base class need to have a virtual destructor here if the derived class allocates no raw dynamic memory?

The following code causes a memory leak: #include <iostream> #include <memory> #include <vector> using namespace std; class base { void virtual initialize_vector() = 0; }; ...
1
vote
1answer
89 views

How to efficiently access public static variables/methods of the correct derived class?

I have an arbitrary number of derived classes all inheriting from the same base class. These derived classes all have the same static variables and static methods, although the implementations may ...
0
votes
2answers
89 views

Should we override all method overloads

Usually method overloads delegate their parameters to the more detailed overloads with default values. here is an example A(x) => A(x, null); A(x, y) => A(x, y, null); A(x, y, z) => ...; ...
0
votes
1answer
92 views

Java convention - Implementing two similar functions for two different objects

I have two classes, let's call them Foo and Bar. They both extend different classes (Foo extends X, Bar extends Y), which have some common ancestor "way up" the inheritance tree, something like this: ...
39
votes
5answers
7k views

Why inherit a class and not add properties?

I found an inheritance tree in our (rather large) code base that goes something like this: public class NamedEntity { public int Id { get; set; } public string Name { get; set; } } public ...
-4
votes
1answer
165 views

Inheritance of Constructors from parent into child class [closed]

Is the Default Constructor of Super or Parent class Can be Inherited into Child class ?
-2
votes
3answers
176 views

Choosing between the lesser of two evils — bad design?

I have two classes, Bar and Baz. Both subclass the abstract class Foo which has a method doThing(). Bar implements a functional interface Now which contains method doThingNow() whereas Baz implements ...
1
vote
3answers
144 views

subclass with no logic of abstract class

I have a class named Change which should be abstract and have some basic methods. I have classes Insert, Update and Delete that extends Change. In the case of Insert, Update I just use extend and add ...
-2
votes
1answer
71 views

looking to solidify understanding of OOP principles

I just wanted some feedback on my definitions of these terms. Abstraction - only showing relevant information and hiding implementation details. Interfaces and abstract classes are an examples of ...
3
votes
1answer
259 views

Architecture for messaging library C++

I have a library, which sends and receives a set of binary messages and parses them. So far I’ve used inheritance for my design, i.e. class BaseMsg { // init msg from rx'd binary stream ...
1
vote
2answers
188 views

Unit testing similar wrappers - or single unit test is enough?

I'm working on a service which has a lots of dependencies. The way how I call them is I'm wrapping each service client around an Adapter. Like this (I'm using Java): public abstract class AdapterBase&...
3
votes
1answer
80 views

Redesign Issue (Multiple Inheritance without Multiple Inheritance)

I am currently working on a game engine for work and have hit a wall on my idea. I have multiple objects (called prefabs) that have very common properties such as Position, Rotation, and Scale. There ...
0
votes
1answer
146 views

Parameters vs Specialized functions

Trying to figure out which structure is cleaner between using a specialized function or using parameters to accomplish the same thing (see code example below). Essentially, I can reduce the number of ...
-3
votes
3answers
135 views

OOP and class that inherit from many classes

In OOP, is it possible to have a class that inherits from multiple other classes? And if yes, how is this situation called? For example, suppose I have the following classes: Eye Nose Mouth Eyebrow ...
0
votes
1answer
70 views

Representing classes hierarchy in database (JPA Inheritance)

I'm working on staff management application for a clinic, basic scenario, but learning about JPA inheritance lead me to over think some parts, and I need help to clear my thoughts and design. ...
0
votes
2answers
198 views

Design issue with delegation, inheritance and dependency injection

My question relates to usage of delegation together with inheritance and dependency injection. I have a MailerService class that requires a delegate in order to do its job. Furthermore, I have a ...
4
votes
2answers
215 views

How should I sub class a class that constructs its objects primarily using Static methods

I have a class, that takes a lot of esoteric parameters to construct an object. I didn't write the code and frankly speaking, I don't understand completely, all of it's nuances. There is a valueOf(...
0
votes
1answer
283 views

How to structure similar structs using Go and PostgreSQL without too much duplicate code

I am making a REST API in Go using PostgreSQL. Quick introduction: I suddenly have a case where I have different variations of the same basic entity, one of the variations have maybe 12 extra fields ...
1
vote
3answers
81 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. ...
0
votes
3answers
341 views

Is there a programming language with objects and generics, but without inheritance?

The GO programming language has objects but no inheritance. It also has generics for the built-in types Array and Map. Is there a programming language with objects but no inheritance and with generics ...
0
votes
3answers
187 views

Base class responsibility

can base class contain protected methods that are not to be used by all descendant classes? I have this situation: EntityA: has some methods that handle activities EntityB: has some methods that ...
0
votes
3answers
192 views

Implementing interface using class members

This was just a question I was wondering about in terms of best practice. I am writing a small C++ program that uses an interface to implement a function. In the class that uses the interface (i.e. ...
2
votes
3answers
425 views

How do I put objects of different types derived from a common base in the same array? [closed]

I have two classes called Player and Enemy both inheriting from a class called GameObject, how do I store them both in an array of type GameObject while still keeping their own unique information? I ...
3
votes
5answers
244 views

Does deciding about Liskov sub.princple compliance require a contract?

I have read two opinions on the subject. Let's assume the following simple code: class Enemy { public virtual void CheckHealth() { if (Life <= 0) Dispose(); } } ...