Stack Exchange Network

Stack Exchange network consists of 174 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

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
77 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
41 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: ...
30
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
75 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
164 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
113 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
62 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
166 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
138 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
63 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
98 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
121 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
39 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
97 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
213 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
0answers
66 views

Is subclassing the way to go if I just want to add one method?

My question is Python-specific. Let's say I am working with an object from a Python third-party library. The object takes some arguments and generates HTML content. The object also has a save method ...
0
votes
1answer
258 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
75 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
33 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
1answer
170 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
169 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
134 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
110 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
232 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
80 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
539 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
66 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
216 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
538 views

Should I still prefer composition over inheritance if I need both the interface and class members be generic?

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
227 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
1k 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
153 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
740 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
184 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
87 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
191 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
112 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
219 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
25 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 ...
-1
votes
1answer
794 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 ...
1
vote
3answers
131 views

Strategy Pattern and inheriting boilerplate functionality

I've read that when using the Strategy Pattern, you should stick to composition rather than inheritance. But does this mean that any inheritance at all is bad practice? If there is functionality to be ...
2
votes
4answers
177 views

Should a base class implement a virtual method for the most common type or derived class?

Is it okay to implement a virtual method in a base class because I know that the majority of derived classes will use that particular implementation? Suppose I have the following class hierarchy (in ...
3
votes
3answers
772 views

How to avoid code duplication while extending two umodifiable classes

I already have this core class structure that can not be changed: class A { //some basic fields and methods } class B { //some another basic fields and methods } It is core classes and I'm ...
1
vote
2answers
3k views

Is it antipattern to use React.cloneElement to extend an element?

I'm creating a popover component for a UI in React. That component contains a button that triggers the popover to display. Because the button needs to be configurable--label, classes, properties--I ...
0
votes
1answer
202 views

Using methods specific to a subclass

I came across this SESE page: What is a proper use of downcasting? (for C#) -- and it talks about when to downcast, and the possible downsides to it. In Python sub classes are slightly different ...
2
votes
1answer
88 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 ...
0
votes
2answers
274 views

Classes as parameters

I would like to write a data structure implementation in Java that uses caches as a core part of its functionality, and I would like the user to be able to provide their own cache implementations that ...
36
votes
3answers
10k views

Is “composition over inheritance” violating “dry principle”?

For example, consider I have a class for other classes to extend: public class LoginPage { public String userId; public String session; public boolean checkSessionValid() { } } and ...
1
vote
3answers
229 views

design pattern for class with data attached

I have a class Tiles that looks something like this: class Tiles { public: void AddTile(int x) { tiles_.push_back(x); } std::vector<int> tiles_; } Now I want to create a class Tiles with ...