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
2
votes
3answers
100 views

Inheritance vs composition: How would you implement an “unordered list”? Subclass of list, or composition?

This came up at work and left me thinking about the best way to model this: In Python, we have the built-in list container, which is a mutable sequence. Equality between two lists is defined as ...
0
votes
2answers
108 views

Object Oriented Design for chess

Recently I came across some article on Chess OOPS design.Following is some snippet from it: public class Chess { ChessBoard chessBoard; Player[] player; Player currentPlayer; List<...
1
vote
3answers
192 views

How is inheritance possible with immutability?

The title almost sums it up but let me be example-led and clearer. Assuming we have a class: class T: def goto(self, value): print('go') And a second inheriting class: class A(T): def ...
1
vote
3answers
182 views

Java Inheritance Problem

I have the finance application where we can have different types of transactions, like balance enquiry, funds transfer, and pin change etc. Now the Transaction is a base class, and there are specific ...
1
vote
0answers
93 views

How can I improve this API solution

Intro I'm creating an app and I'm not sure if the structure of the solution is correct. I have a BaseController that uses generics. This controller is inherited by others that do not have to ...
-4
votes
1answer
55 views

How could I redefine a new type for subclass

I'm trying to represent some entity. LineProduct A line holding a quantity of product and a Product entity LineProductBlank A line holding a quantity of product and a ProductBlank entity Product ...
-4
votes
1answer
58 views

What's the best way to design a state machine with different object inheritance

I have a class A and class B, both derive from the same parent class. Each object has a state machine inside it that defines it's behaviour. The behaviour is different though depending on if the ...
0
votes
1answer
84 views

How can I have two interfaces implement one interface?

I have device interface classes written in c# for electronic variable loads. They are mostly for DC devices. We got a new one that does loading for both AC and DC devices. I also have an interface ...
5
votes
1answer
376 views

Why were default methods introduced to Java?

Was introducing default methods to java inevitable? As far as I know multiple class inheritance was not introduced to avoid difficulties with the method signature clash in base classes. So we avoided ...
1
vote
1answer
105 views

Strategy Design Pattern vs Inheritance

I have to write a code where i have send some data (call it List of cases) using one of two integration tools (Jitterbit, Mulesoft), with possibility of adding more tools in future. Integration tool ...
3
votes
2answers
128 views

Composing and Inheriting from the Same Type

To start off with an example: I have a read-only Repository used for getting arbitrary values. This behavior can be implemented multiple ways. I also want to allow opt-in mutation of the repository's ...
2
votes
3answers
173 views

Enums or Multiple Inherited Classes

I am reading about OOD and came across Parking lot design problem.Parking lot has parking floors which has parking spots.The parking spot class looks as follow: public enum ParkingSpotType { ...
2
votes
2answers
152 views

Is switching sub-class at runtime a proper use case for inheritance?

I've got a DataSource base class and a few different sub-classes. I want to be able to switch which sub-class i'm using at runtime so I'm doing the following : // pointer for our datasource object ...
18
votes
8answers
5k views

Is it ok to inherit a class without adding anything to the child, to respect the Open Closed principle?

To clarify the question, here is my context (or something very similar). I have an interface, that I call IDataSource. The implementing classes contain information to retrieve data. So I have multiple ...
-4
votes
1answer
41 views

Alternatives to inheritance for polymorphic behavior for data in database?

I am thinking of designing a website where people can enter their aliases for games they played in the past and they can search for others around the same time period. FWIW I don't care if it exists ...
0
votes
4answers
350 views

Why have separate keywords for 'extends' and 'implements' in Java? [closed]

Short answer that I've come to accept: Firstly, it helps with readability, being able to see which is the superclass apart from interfaces. Secondly, though 'extends' and 'implements' do the same ...
-2
votes
1answer
123 views

What is the point of inheritance in c++ programming? [duplicate]

Classes in c++ can be extended, creating new classes which retain characteristics of the base class. This means we can create something like this : struct Person { struct Person** children; ...
-2
votes
1answer
46 views

Inheritance and domain-specific logic loses its independency?

Im sorry in forward, Im sure I wont be able to express myself well. Its a game system: abstract class GameType { private $score; public __constructor($score) { $this->score = $...
-3
votes
1answer
81 views

In Java API documentation, what does it mean to inherit an abstract method? [closed]

Here's an example: In the documentation for java.util.HashSet, there's a list of "Methods inherited from interface java.util.Set", including equals, which is an abstract method in java.util....
0
votes
4answers
212 views

C# refactoring with inheritance

I have two classes which contains almost same method. How can I refactor this with inheriting one class by other class. class A{ public void run(){ // task 1 // task 2 // task 3 } ...
0
votes
1answer
89 views

Are inheritance, abstraction and polymorphism regarded as association in OOP?

In the realm of OOP, inheritance, abstraction and polymorphism are basic concepts of the paradigm. On top of that, there also have the concept called "associations" which I understand is ...
3
votes
5answers
233 views

Should I create different classes for the same technical object but different business usages

I know my question sounds a bit weird so I will try to explain my issue with examples. In my application, I have two types of business objects : Shops and Customers for example. Both of them can be ...
0
votes
2answers
101 views

How to share entity classes (JPA) among projects, where not all have DB access?

So, I have project in which several WebServices will be created (REST). For the sake of simplicity, Lets name them A, B, and C. A and B handle different tasks, but both of them consume C, which is the ...
7
votes
3answers
204 views

Is a subclass Liskov Substitutable if it disallows the same invocation of the constructor in the child as in the parent?

Say, I have two classes: class FTPClient: def __init__(self, host, port): self._client = FTPDriver(host, port) ... class FTPSClient(FTPClient): def __init__(self, host, port, ...
3
votes
2answers
221 views

OOP - How to identify inheritance abusage?

In looking for a simple heuristic to see when inheritance can be abused, I came up with the following hypothesis: If subclass B overrides method foo, but does not call base.foo(), it seems like ...
-3
votes
1answer
61 views

How would you architect a simple cascading style sheet object?

How would you architect a simple cascading stylesheet like inheritance object? For example, I have Apple that extends Fruit. class Fruit { constructor() { this.total = 10; } } ...
3
votes
4answers
163 views

Extension points via inheritance vs via delegate fields

In C#/.NET, I have a class that I want to provide extension points for. I can do this either using inheritance: public class Animal { public virtual void Speak() { } } public class Dog : Animal { ...
0
votes
1answer
76 views

Inheritance but with two different shared traits?

I am facing a dilemma with designing an api gateway. Currently, I am using the pipeline pattern, with different stages being the requests made to various services, (http, sockets, amqp, ...). I have ...
1
vote
1answer
36 views

what kind of relationship do child components have with components in react?

I'm currently writing a paper on my react frontend and I'm struggeling to find the right verb for the interaction between child-components and components in react. For example: "I have a table ...
0
votes
2answers
199 views

Liskov substitution principle: clarification about the “history rule”

I`m trying to understand the LSP History rule. I have read Wikipedia entry which states the requirement and provides an example: History constraint (the "history rule"). Objects are regarded as ...
28
votes
7answers
9k views

Polymorphism case study - Design pattern for morphing?

Imagine two classes: class Person { MarriedPerson marry(Person other) {...} } class MarriedPerson extends Person { } The idea is that a Person is automatically "morphed" to type MarriedPerson ...
-1
votes
4answers
212 views

Is inheritance without polymorphism/overriding a right practice?

Consider a SimpleCalculator class which contains four methods (Add, Subtract, Multiply, Divide). I need to create another class ScientificCalculator which needs above methods plus other methods like ...
0
votes
2answers
165 views

Inheritance: Folders and Files & Liskov Substitution Principle

Based on what I have been reading about the Liskov Substitution Principle, I understand that a square and rectangle class cannot be a part of the same inheritance tree. I would like to apply these ...
0
votes
1answer
168 views

favor composition over inheritance: practical problems

In a C# application, I've got a behaviour that I would like to be available in different classes that not necessarily share the same ancestor. What better opportunity to 'favour composition over ...
0
votes
4answers
226 views

Open closed principle: code duplication and encapsulation

In another question on this site, asking to clarify the open closed principle, @Kate Gregory answered this. I'm interested in this part specifically: Imagine you wrote an Invoice class that works ...
1
vote
1answer
134 views

Rails model inheritance without STI or polymorphism?

I have a Rails app with HighSchoolTeam and ClubTeam models. I'm currently using Single Table Inheritance (STI) with Team. That means I only have a Teams table in my database, no High_School_Teams or ...
0
votes
1answer
306 views

Why does php not allow to decrease visibility of class properties and methods in the inheriting class?

Please consider the following code: class baseclass { public $hideme; public function getit() { return $this->hideme; } public function setit($value) { $this->hideme = $value; } } ...
5
votes
1answer
144 views

Selectively exposing interface of inner members

I have a class called D2Array which represents a fixed-size 2D array. It's meant to be generic and it comes with quite its lot of methods: getting an element, setting an element, extract a whole row, ...
0
votes
3answers
199 views

How to inherit a static function in a class?

Probably the answer is you can't. However, I would like to have a work-around to solve my problem. Objective I am trying to create a program in which I try to avoid nulls as much as possible. ...
2
votes
1answer
160 views

Late inheritance, enforce subclass behavior in the future

A super class Transaction has two subclasses TransactionA and TransactionB. A Transaction is composed of multiple events that occur in time for a specific key (a file, a person, etc.). Depending on ...
1
vote
1answer
91 views

How to simplify work with class inheritance when a lot of arguments needed to be passed to c-tor?

Let's say I have class that used inside another class and uses inside some nested fields of it. To simplify the example I'll use trivial types. In reality it's something like DataType dataType, Status ...
2
votes
2answers
195 views

How to design different moves of different pieces of a Chess game using OOP principles?

I am trying to design a Chess game where each piece of the game inherits from an abstract Piece class. The piece just does a few things: It tells which color it is (White / Black) It tells what type ...
1
vote
2answers
85 views

Python subclassed methods with additional arguments

Given the following structure where I want a method with a common name for each class, but each derived class needs an additional piece of information in order to form the appropriate result. ...
0
votes
2answers
186 views

Should parent classes define methods using attributes of child classes?

I need to extend the parent classes of two child classes by adding shared methods to them. Starting with the initial definitions of the child classes: class ChildA(ParentA): pass class ChildB(...
1
vote
2answers
104 views

Need good design: Anemic Model, Inheritance and Pattern Matching

I have Handler classes which accepts Queries and returns Results. Handlers is anemic. They accept input data bag and returns output data bag. Handlers can be many so I created common generic interface ...
18
votes
11answers
10k views

Need Good OOP Design For World and Countries Problem

I am currently working on problem with a chicken or egg first situation. Basically, I am designing a solution which goes like this: World is a collection of countries; Each Country has a name, flag ...
4
votes
1answer
560 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
170 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 ...
8
votes
3answers
4k 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
662 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 ...

1
2 3 4 5
11