Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

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.

0
votes
2answers
170 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
44 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
69 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
186 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
61 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
116 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 ...
8
votes
2answers
366 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
80 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
88 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
70 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
6k 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
107 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
171 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
124 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
66 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 ...
2
votes
1answer
225 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
184 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&...
2
votes
1answer
76 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
115 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
128 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
46 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
125 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
214 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
274 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
77 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
1answer
35 views

ViewConfig inheritence hierarchy for views

I need some help with the hierarchy of view configs, which populate my views (templates). Maybe view config is not the correct term here, it's just the one I'm using, maybe view state makes more sense?...
0
votes
3answers
306 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
180 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
153 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
206 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
237 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(); } } ...
0
votes
2answers
93 views

Base class with common properties

I have multiple classes that have the same properties to be exposed. Is it normal to create a base class, which will expose those properties, to be used with descendent classes? By properties I ...
2
votes
1answer
1k views

C# design to force the virtual method to be called from the override, or something similar

We can do something like this: // in base class protected virtual void Init(){ //do stuff that all objects need to do } //in derived class protected override void Init(){ base.Init(); // if ...
1
vote
1answer
69 views

Handling generic and specific implementation

Normally we have some code that is common for many (if not all) derived classes. An example would be a generic repository (where I could have a generic Add, Remove, Count, Max, Load etc, or a generic ...
1
vote
3answers
233 views

Rethinking inheritance when subclass implements interface

Suppose if I have an abstract Weapon class, and the subclass ReloadableWeapon which implements the interface Reloadable. interface Reloadable { void Reload(); } public abstract class Weapon{ @...
2
votes
6answers
804 views

Should I still prefer composition over inheritance if the child classes need BOTH the parent interface AND its class properties?

According to Why should I prefer composition over inheritance?, I should prefer composition over inheritance. But what if I need to access the interface and class member in generic way? For example, I ...
2
votes
4answers
327 views

Inheritance best practice?

I'm fairly new to object oriented programming and have a question I've stumbled upon regarding inheritance best practice. I'm building a system for weapons in a game. I have a base class called ...
-2
votes
2answers
214 views

Can we say Apple inherits from both Fruit and Seed classes?

Given three classes Apple, Fruit and Seed ; Apple is a kind of Fruit and it has Seed in it. With the knowledge of Apple Class uses directly or indirectly the properties of Fruit and Seed. Can we say ...
1
vote
4answers
2k views

Why the industry prefer/use composition over inheritance? [duplicate]

I was having a discussion with few friends about inheritance and composition and what I learnt from that discussion is that the use of inheritance more or less condemned in industry nowadays and ...
1
vote
2answers
161 views

How to model my Child Classes without violating OO Principles

I am trying to model an external system in Java and running in to some issues. I have a handful of related types that I have mapped together through abstract (and sometimes concrete if it made sense) ...
0
votes
1answer
1k views

Workaround for casting method parameter interface to desired concrete type in polymorphic design?

Let's say that I have a group of report types to be created and all of them inherit from base abstract class: abstract class Report { public abstract void GenerateReport(IReportData data); ...
0
votes
1answer
189 views

Polymorphism/Inheritance design concerns

I have a FtpServerDataSeriesProvider that is used to provide data series from Ftp server files: public interface class FtpServerDataSeriesProvider { IEnumerable<DataSeries> GetDataSeries();...
0
votes
1answer
102 views

Alternatives to inheritance for components with different behaviours?

I have been learning a lot of inheritance in C#. But am wondering if there are other ways that can be cleaner for component based setups. I have objects that are all pretty much the same but they ...
4
votes
3answers
200 views

Where to put the the composition?

In the following diagram I have a Car which has a Motor. An Audi A6 is a Car which has a Motor of type 2.4 L V6. So both the Car and the Audi have a composition on a motor. However it looks ...
-1
votes
1answer
113 views

How to Model below Hiearchy with OOP

Note that I cannot use static inheritance due to language limitations (Java). There is a general Building class. Each instance of Building has properties that exist regardless of instance variables (...
1
vote
5answers
237 views

Inheritance as a specialization

I have a class called Book with fields such as title, type etc. I also have a class called Library that manages books. Library has methods that: Add a copy of a book on a shelf Move a copy of a ...
0
votes
0answers
26 views

How to organize classes which should reflect different versions of standard?

I have some standard which changes in time. Let's say I have std_v1, std_v2 and std_v3. Those standards have a lot of common parts with some little differences, possible additions and deletions. On ...
0
votes
2answers
1k views

C#, Static classes and Inheritance

As a beginner I'll try to explain my problem as good as I can: I'm currently trying to program a "simple" ECS. My basic idea is that I have a base "Entity class" which includes all sorts of functions ...