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.

-3
votes
3answers
119 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 ...
1
vote
1answer
29 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
1answer
64 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
174 views

How should I sub class a class that constructs it's 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
63 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
228 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
73 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
164 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
166 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
129 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
96 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
222 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
74 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
311 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
65 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
213 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
465 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
209 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
213 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
776 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
132 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
466 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
183 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
81 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
190 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
108 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
209 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
647 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
119 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
169 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
594 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
2k 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
172 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
87 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
215 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 ...
0
votes
4answers
133 views

How to insert code in a method in a derived class [duplicate]

I have a base class with a method called Update: Start Update Code block 1 (An If statement) Code block 2 (Setting a variable based on the If result) Code block 3 (A switch which is ...
2
votes
2answers
93 views

Creating a modified copy of an instance of an abstract interface

Say I have an interface Interface, which only contains getters for various fields. This interface has multiple implementations (say Foo and Bar), each of which adds various fields. All these ...
3
votes
3answers
159 views

Liskov substitution for voids and weakened preconditions

I am learning a lot about this principle (also thanks to two answers I received here) and would like to elaborate on another point that somebody mentioned. 1) Is the following a violation of LSP? ...
3
votes
5answers
402 views

Why don't OOP languages offer a feature to clone a parent into a child class?

Consider the following c# example: public class MyParentClass { public int MyInt { get; set; } } public class MyChildClass : MyParentClass { } public class AnotherClass { public MyChildClass ...
7
votes
2answers
2k views

Proper design to avoid the use of dynamic_cast?

After doing some researches I can not seem to find a simple example resolving a problem I encounter often. Let's say I want to create a little application where I can create Squares, Circles, and ...
1
vote
1answer
78 views

Abstract base class implementing an interface, providing a different one to its subclasses

Let's say I have an interface for the Command pattern with undo functionality: interface ICommand { void Execute(); void Undo(); } This comes with a contract. Basically the command can be in ...
1
vote
1answer
106 views

Inheritance or strategy in my circular buffer

I've looked at some other threads but they don't quite answer my question, I think... I have a CircularBuffer object which I have separated from a ThreadSafeCircularBuffer object so that the CB logic ...
2
votes
2answers
182 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 { /// <...
5
votes
2answers
224 views

Should We Use Protected Member in Base Class Or Private Member in Derived Class?

The reasons may vary from breaking principle/guidelines of OO to considering protected as part of public API. Then, what should we do if some derived classes have same members? Should we declare them ...
1
vote
1answer
83 views

Client to application interaction results in fat interfaces

How do you handle fat interfaces? Here is an example: public class TSP { public AddEmployeeContribution(...) {...} public AddMatchingContribution(...) {...} public CalculateTotal() {...} ...
0
votes
1answer
338 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(...