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
1answer
85 views

Selective method inheritance

I have a lot of classes that are just a CRUD interface for microservices. They only have a param for the endpoint and some of the methods get_list / get_item / create / update / delete / activate / ...
1
vote
2answers
130 views

Can you define node pointers in a base binary tree class?

I've created two separate binary tree classes, with some shared functions/variables and some that are not shared. So I have tried to abstract away the similarities in a base BinaryTree class. class ...
2
votes
2answers
128 views

How to structure classes for two distinct use cases that share key parameters

I have a Java application that needs to generate mathematically-defined 3D shapes for a voxel world (Minecraft specifically, but that's not important to the discussion). These include sphere, ovoid, ...
1
vote
3answers
146 views

How to create uniform interface of C++ classes without virtual methods?

I have been developing control software in C++. My hardware consists of a microcontroller with an integrated a/d converter and an external on board a/d converter. Both of these a/d converters have ...
1
vote
2answers
93 views

Is it wrong to extend an inner static class in java? (Builder pattern)

I'm working on a java project for the university. The project is a card game in which you travel around a 2D map and fight against some enemies. My part consists of creating the deck and the cards. I ...
0
votes
1answer
48 views

Composition without direct instantiation through constructor

I have 2 classes that independently operate on specific file formats: class FileWriterA and class FileWriterB. Each class has some shared methods (e.g., __enter__, __exit__, __str__, closed, is_closed,...
0
votes
1answer
63 views

Inheritance and association relationships in UML

I was creating a class model using inheritance, and have a situation I don't know how to represent. For this question I built an example diagram in which I have a class Person with some attributes. I ...
3
votes
3answers
764 views

Use inheritance to make single child, smaller

We have a relatively big class. One of my colleagues thinks we must split this class into a base & child class to make it smaller and cleaner. On the other hand, I believe when we know this parent ...
2
votes
3answers
112 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
128 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
216 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 ...
0
votes
3answers
199 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
95 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
59 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
73 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
86 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
387 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
118 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
129 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
175 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
154 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
58 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
364 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
126 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
83 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
246 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
100 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
236 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
207 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
207 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
222 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
62 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
166 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
38 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
214 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
220 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
168 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
180 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
244 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
214 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
411 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
146 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
200 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
226 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
2 3 4 5
11