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.
517
questions
1
vote
3answers
117 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
88 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
38 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
60 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
756 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 ...
36
votes
5answers
3k views
How is defining that a method can be overridden a stronger commitment than defining that a method can be called?
From : http://www.artima.com/lejava/articles/designprinciples4.html
Erich Gamma: I still think it's true even after ten years. Inheritance is a cool way to change behavior. But we know that it's ...
2
votes
3answers
108 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
118 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<...
5
votes
2answers
2k views
Database design for polymorphic data
I have an application that needs to log communications with users over several different mediums: Email, SMS, Voice, Website Announcements, etc.. in a traditional database.
I have considered 3 ...
1
vote
3answers
203 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
192 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 ...
0
votes
1answer
235 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 ...
1
vote
0answers
94 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
57 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 ...
3
votes
1answer
257 views
DDD: Type hierarchy of entities' property
Say I have an Delivery class which should have a Destination property (among many others). Destination can be an Address (country, city, address line, post code) or a Port (international code, name). ...
2
votes
3answers
174 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 {
...
-4
votes
1answer
64 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
2answers
138 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 ...
0
votes
1answer
85 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
379 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 ...
8
votes
4answers
19k views
Having an inherited function return the derived type instead of the base type
I am writing two classes in C#:
A Matrix class that represents a general Matrix with n-by-m dimensions
A SquareMatrix class that inherits from Matrix and has the constraint of being n-by-n
The ...
1
vote
1answer
112 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
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
49 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 ...
4
votes
3answers
344 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?
...
0
votes
4answers
357 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
125 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
226 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
93 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 ...
40
votes
5answers
9k views
Why inherit a class without adding 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 ...
3
votes
5answers
235 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 ...
7
votes
3answers
206 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
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
164 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 ...
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 ...
0
votes
2answers
202 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 ...
3
votes
1answer
4k views
How to avoid having nested generic in class
I'm working on a side project, and I turned on all rules for code analysis in Visual Studio, and I got the warning notice:
Warning CA1006 Consider a design where 'Vote<T>.CalculateWinner(...
1
vote
1answer
182 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 ...
-1
votes
4answers
215 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
166 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
174 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 ...
12
votes
2answers
7k views
How should I handle docstrings of subclass methods?
I have an abstract class structured somewhat like this:
class AbstractQuery(object, zip_code):
def execute(self):
"""
Retrieve information about a zip code
The information ...
0
votes
4answers
235 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 ...