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.

learn more… | top users | synonyms

27
votes
8answers
3k views

Is creating subclasses for specific instances a bad practice?

Consider the following design public class Person { public virtual string Name { get; } public Person (string name) { this.Name = name; } } public class Karl : Person { ...
-3
votes
1answer
115 views

Is this a good use of a base class? [closed]

I am developing some HW drivers, which access registers. I have a few classes, for example. ClassRegisterBlock1, ClassRegisterBlock2. In each of these classes, they define the bits to manipulate, ...
1
vote
2answers
217 views

How to avoid redundant code in designing inheritance in C++

Say I have a Base class called Car and it has 3 derived classes say Ford, Honda and Audi. The issue is, all three derived classes have exactly same code but minor difference in calling member ...
1
vote
1answer
52 views

Is doing Parent.call from the child object a right way to implement Inheritance in JavaScript?

I came across this piece of code it didnt look that right to me. Is it the right way to implement SUPER in javascript. if not what is the right way? function Person(name){ this.name = name; } ...
1
vote
2answers
217 views

Java: how can I make the return type, of an inherited method in a subclass, the same as the subclass?

I am rather inexperience in Java, and I'm having a problem in forming a subclass of a class I have created. The class I have made, called Vector2D, contains methods, such as add(Vector2D addend), that ...
2
votes
2answers
180 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 ...
3
votes
1answer
89 views

Augmenting functionality of subclasses without code duplication in C++

I have to add common functionality to some classes that share the same superclass, preferably without bloating the superclass. The simplified inheritance chain looks like this: Element -> ...
1
vote
1answer
212 views

Implementation of instance testing in Java, C++, C#

For curiosity purposes as well as understanding what they entail in a program, I'm curious as to how instance testing (instanceof/is/using dynamic_cast in c++) works. I've tried to google it ...
1
vote
2answers
394 views

One boilerplate class or many similar classes?

Lets say I'm trying to model a variety of objects that are virtually identical, the only difference being their class variables. Am I better off creating one boilerplate class and just calling the ...
1
vote
1answer
52 views

Is table inheritance the wrong approach

If we take the two below entities as en example. public class Person { public string Username {get;set;} public string DisplayName {get;set;} } public class Worker:Person { public string ...
2
votes
0answers
51 views

Traits, Inheritance and Interfaces in Groovy, when to use them?

I'm learning groovy and I've just learned about the new feature added in 2.3, which is the addition of Traits. Now to me it seems like Traits allow you to do basically everything a super-class and an ...
4
votes
2answers
862 views

How to replace inheritance with composition in this case?

I've recently read several articles about the advantages of the composition over inheritance. Their authors said that you can always replace inheritance with composition (to be precise, they say they ...
1
vote
3answers
91 views

UML Class Diagram: How can I represent “orthogonal” generalizations (or multi-inheritence)?

I try to represent in a UML Class Diagram the following object and features: The Object: A System (e.g. an electronic device) that can be of Type A, B or C If the System is of type A, it has a ...
0
votes
1answer
26 views

Representing the hierarchy of groups of numbers using type inheritance

I've run into an interesting conundrum while coding my own implementations for the basic sets of mathematical numbers (Natural, Integer, Rational, Irrational, Real, Complex). I'm doing this mostly for ...
2
votes
1answer
87 views

Is this approach to CSS correct?

Reading SASS basic features on their website, I stumbled upon the @extend feature. The example they give is the following: .message { border: 1px solid #ccc; padding: 10px; color: #333; ...
2
votes
0answers
91 views

JavaScript Combination Inheritance Pattern

In the chapter of Professional JavaScript for Web Developers on OOP, Nicholas Zachas describes a JavaScript inheritance pattern which he refers to as combination inheritance. The basic idea is that ...
21
votes
1answer
454 views

How are mixins or traits better than plain multiple inheritance?

C++ has plain multiple inheritance, many language designs forbid it as dangerous. But some languages like Ruby and PHP use strange syntax to do the same thing and call it mixins or traits. I heard ...
2
votes
2answers
123 views

Wrapping constructor arguments

Given a base class with around 25 or so subclasses, I have found that adding an argument to the base constructor is painful. protected AbstractController(Service1 s1, Service2 s2, Service3 s3){ ... } ...
2
votes
4answers
150 views

Subclass only have methods without variables

Is it ok to have subclass which does not declare its own variables (instance variables) and inherits from super class or base class? It has methods and there are some common methods which it overides ...
2
votes
5answers
322 views

Classic inheritance problem?

I keep seeing the following pattern when people learn about OOP: Problem: How do I put objects of different but related types into a container? Solution: Inherit from a common base class. New ...
2
votes
2answers
80 views

subclass reference to another subclass

Imagine I have the following code: class A: pass class B(A): pass class C(A): def __init__(self): self.b = B() Is the above code correct in terms of correct inheritance? I ...
1
vote
2answers
128 views

A vector of pointers to different derived classes of the same base class

I am writing a game in C++ which has different enemy types. I have defined a base Enemy class and I also have derived classes enemy1, enemy2 etc. Now for updating these enemies in each iteration of ...
1
vote
1answer
65 views

Adding new functionality to all of shelve.Shelf's subclasses in Python

In order to avoid the overhead associated with the shelve module's writeback option I'm interested in putting together a shelf class that only accepts hashable values, with hashability being a proxy ...
0
votes
1answer
93 views

Doctrine 2 and Concrete table inheritance

I use Doctrine 2 and I've read some articles on inheritance strategies' mapping with ORM. I've seen three main strategies : "Class table inheritance", "Concrete table inheritance" and "Single table ...
3
votes
1answer
69 views

Why is subclassing TraversableOnce not recommended

Reading http://www.scala-lang.org/api/2.11.1/index.html#scala.collection.TraversableOnce: Directly subclassing TraversableOnce is not recommended - instead, consider declaring an Iterator with a ...
2
votes
2answers
252 views

Code re-use in C++, via multiple inheritance or composition? Or…?

I originally asked this question on StackOverflow, but I was directed here, and I think my problem is perhaps as much conceptual as technical, so here goes. If you’re defining a hierarchy of abstract ...
1
vote
1answer
54 views

Public versus private inheritance when some of the parent's methods need to be exposed?

Public inheritance means that all fields from the base class retain their declared visibility, while private means that they are forced to 'private' within the derived class's scope. What should be ...
0
votes
1answer
56 views

Matching the superclass's constructor's parameter list, is treating a null default value as a non-null value within a constructor a violation of LSP? [duplicate]

I kind of ran into this when messing around with FlashPunk, and I'm going to use it as an example. Essentially the main sprite class is pretty much class Entity. Entity's constructor has four ...
1
vote
3answers
215 views

What alternative is better to diagram this scenario?

I was creating and discussing a class diagram with a partner of mine. To simplify things, I've modify the real domain we're working on and made up the following diagram: Basically, a company works ...
3
votes
1answer
259 views

How to maintain encapsulation with composition in C++?

I am designing a class Master that is composed from multiple other classes, A, Base, C and D. These four classes have absolutely no use outside of Master and are meant to split up its functionality ...
0
votes
0answers
43 views

Example of persisting an inheritance relationship using ORM

I have some experience with OOP and RDBs, but very little exposure to web programming. I am trying to understand what non-trivial types of problems are solved by ORM. Of course, I am familiar with the ...
5
votes
3answers
211 views

How to refactor my design, if it seems to require multiple inheritance?

Recently I made a question about Java classes implementing methods from two sources (kinda like multiple inheritance). However, it was pointed out that this sort of need may be a sign of a design ...
0
votes
2answers
269 views

Can an object call a method on it's immediate descendant? [closed]

I have an inheritance hierarchy three deep : an abstract base class, from which I derive another abstract class, call it the intermediate class, from which will derive possibly a large variety of ...
0
votes
1answer
97 views

overriding implemented base class methods

I read somewhere that the chain of inheritance breaks when you alter a behavior from derived class. What does "altering a behavior" mean here? Is overriding an already implemented method in base class ...
3
votes
4answers
246 views

Why is there no 'subclasses-only' access modifier in Java?

In Java, there are four available access modifiers for methods: public - any class can use this method. protected - classes in the same package and subclasses in any package can use this method. ...
7
votes
4answers
274 views

Is inheritance that adds rules bad? [duplicate]

I got into a debate on this question which distilled to if it is a good idea for a specialization of a class to add business rules. Unfortunately this point got trampled in the comments so I'm asking ...
73
votes
13answers
7k views

Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?

If a Square is a type of Rectangle, than why can't a Square inherit from a Rectangle? Or why is it a bad design? I have heard people say: If you made Square derive from Rectangle, then a Square ...
1
vote
0answers
161 views

Reusable and customizable charting library on top of d3js

I have started building a charting library on top of d3js using javascript's inheritance. My goal is to develop reusable and fully customizable chart components. I read the article: Towards Reusable ...
2
votes
2answers
129 views

Is it appropriate to use inheritance to prevent code duplication of the logic for a user control?

Suppose I have two or more UserControl implementations with vastly different implementations but near identical code-behind. One strategy to avoid code duplication is as follows: Change each ...
2
votes
3answers
289 views

Inheritance: Is code from superclass virtually *copied* to subclass, or is it *referred-to by subclass*?

Class Sub is a subclass of class Sup. What does that mean practically? Or in other words, what is the practical meaning of "inheritance"? Option 1: The code from Sup is virtually copied to Sub. (as ...
1
vote
1answer
174 views

Should I use inheritance or an interface?

So in a nutshell I have an abstract class : public abstract class Member { public string PCode { get; set; } public string Reference { get; set; } public DateTime? ElectedDate { get; set; } } And ...
4
votes
2answers
700 views

What is the process of determining which method in a class hierarchy should execute known as?

I thought I understood inheritance and polymorphism, but I was given this question, and I can't, for the life of me, figure out what the proper answer is or what they're trying to get at: The ...
2
votes
2answers
222 views

Testing abstract class' behavior

I'm currently refactoring an existing design, which was created without TDD. There is a class hierarchy with one abstract base class and two subclasses. In the original design, these classes were ...
1
vote
2answers
92 views

Inheritance vs 'specification' by fields

I'm currently reading 'Head first design patterns', and already the first chapter threw some questions towards me. This chapter introduces the 'prefer composition over inheritance' design principle, ...
0
votes
1answer
108 views

When is it suitable to use inheritance [duplicate]

I recently had a small "argument" about inheritance with a friend. I think people overuse it a lot. Intuition tells me that that the only good reason for class inheritance is polymorphism - when there ...
0
votes
0answers
75 views

subclass QAbstractTableModel - reuse as much code as possible

in my PyQt application I am heavily using the model/view approach. For every model I create, I do the following: class myModel(QtCore.QAbstractTableModel): def __init__(self, parent = None): ...
1
vote
2answers
132 views

Alternatives to inheritance on complex structures?

I'm working on an ASP.NET Webforms project, using the MVP Pattern (which the presenters as you may know use a Page Controller pattern). I'm given a rather complex inheritance chain for my controllers. ...
1
vote
3answers
260 views

Why are inheritance and interfaces restricted to instance members?

Disclaimer: I think the rules are almost the same in most OO languages, but since I'm most familiar with C# I'll be relating to this specific language. I think that the use of attributes and ...
3
votes
3answers
179 views

Why should one subclass instead of composite when using the Factory Method design pattern?

I'm currently learning about design patterns. I learned about the Factory Method pattern. This pattern means that in order to implement a factory to create objects, one should subclass the class that ...
1
vote
3answers
479 views

Should utility base classes be avoided

Knowing that C# doesn't support multiple inheritance, is it considered bad form to write a utility base class? Some initial thoughts: I could create an interface and a concretion, but then the ...