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

1
vote
1answer
98 views

Design Pattern - Adding features to a class

I'm finding myself unable to solve a design problem. For the sake of my simplicity, I'm going to use C# (Web API). Let's say I have an abstract class RestController: public abstract class ...
0
votes
0answers
61 views

Pattern to share fields in inheritance

I'm trying to figure out the best way to solve a design issue. I have to be able to clone (I'm cloning them to Apache HttpClient) different types of HttpServletRequest (POST, GET...) and then send ...
1
vote
1answer
52 views

Repeating Interface inheritance declarations

Take the following example: public interface ISerialRVD { RouteValueDictionary ToRVD(); } public interface IViewModel : ISerialRVD // Here? { Int32 Page { get; set; } String Action { ...
3
votes
1answer
69 views

How should one modularly implement related objects?

I tried to make the question title as generally applicable as possible, but I'm not certain I worded it so well. It was brought about by a very specific problem I'm having, and for the remainder of ...
4
votes
1answer
101 views

Separate interface for mutation methods

I've been working on refactoring some code, and I think I may have taken the first step down the rabbit hole. I'm writing the example in Java, but I suppose it could be agnostic. I have an interface ...
11
votes
5answers
418 views

Should I prefer composition or inheritance in this scenario?

Consider an interface: interface IWaveGenerator { SoundWave GenerateWave(double frequency, double lengthInSeconds); } This interface is implemented by a number of classes which generate waves ...
0
votes
2answers
180 views

Why 'class String' definition has 'final' modifier? [duplicate]

This answer gives an indication that class String is declared final for thread safety, which does not convince. This answer does not convince when it says: And so, you make the classes final. There ...
1
vote
1answer
88 views

If the representation part of type abstraction is declared private, then how does sub-classing violate encapsulation?

Here, I learned that sub-classing violates encapsulation: it violates encapsulation, since the implementations of the superclass and subclass become tightly coupled For instance, If we consider ...
0
votes
0answers
10 views

What's a way to encapsulate “mapping” code and provide reusable functionality in a base class?

I have created a set of repository base classes, using the generic repository pattern. The Repo<T> has CRUD functions incorporated. Some of the View Models need to be mapped (unflattened, etc) ...
0
votes
1answer
55 views

I would like to know if there are special terms for differnt types of inheritance

Are there terms to distinguish these two different types of inheritance? The one type of inheritance is vehicle - car. (these classes have some similarities). But then there is for example ...
2
votes
2answers
259 views

Is this the solution to static inheritance?

I've seen a lot of posts recently on why Singletons should be avoided. However, I can't see any of those problems with the following solution to a common problem: static inheritance. For example, I ...
1
vote
2answers
45 views

Expressing interface inheritance in natural language [duplicate]

In object-oriented programming, when you extend a class you establish an is-a relationship between a new subtype and its parent(s), i.e. B is an A (aka code inheritance). When you compose a class with ...
3
votes
2answers
299 views

How do I override the Type of members in a base class from the derived class?

I have two classes that know each other in a many-to-many relationship. Now I want to have two additional classes that inherit from the existing classes. The association between the inherited classes ...
0
votes
1answer
61 views

How Do I Create A Module* For A Class That Relies On Another Module?

*I'm using "Module" to mean some extension of a class, either through inheritance or composition. Say I have one of the following declarations of an Entity: using EUnit = int; class Entity : /*With ...
2
votes
5answers
145 views

Should I use inheritance to differentiate objects even if they have the same fields?

Consider this simple class that models a real world mobile device: /// <summary> /// Model that represents a device. /// </summary> public class Device { public DateTime ...
1
vote
0answers
119 views

Class Interfaces with Multiple Classes and Dependency Injection

NOTE ON QUESTION TITLE: I did not really even know how to phrase the title so if someone can edit and make it more articulate I would really appreciate it. QUESTION: Basically, what I'm trying to do ...
30
votes
10answers
4k views

Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?

I have a base class, Base. It has two subclasses, Sub1 and Sub2. Each subclass has some additional methods. For example, Sub1 has Sandwich makeASandwich(Ingredients... ingredients), and Sub2 has ...
2
votes
1answer
51 views

Keeping lists of Objects, some with, some without inheritance

Let's say I have two classes, Foo and ExtendedFoo where ExtendedFoo inherits from Foo. Foo has a method .getValue() which returns a number, ExtendedFoo also has .increaseValue() which adds to the ...
0
votes
3answers
203 views

Why does an interface extend an interface instead of implementing it?

In Java suppose that I have interface A: public interface A { // foo } I also have interface B: public interface B extends A { // foo + bar } Why does interface B extend interface A ...
4
votes
2answers
164 views

Why is it bad to have one mechanism for inheritance and subtyping?

It is quite well-known that (class) inheritance and subtyping (sometimes called interface inheritance) are different things: inheritance is a mechanism for sharing code, while subtyping is a ...
4
votes
1answer
42 views

Modular Database, Inheritance of Schema

Consider multiple relational database schemas that have an inheritance dependency structure as known from OOP systems: Tables are just a placeholder for arbitrary database objects such as Views, ...
1
vote
1answer
194 views

Inheritance or Composition?

Let's say I have a main class A and another class B. In the B class the user can change some settings and variables of the program that belongs to A, I want to be able to apply these changes to the A ...
0
votes
4answers
219 views

Fixing class Hierarchy design mistakes due to incorrect inheritance

I have a code like this. My derived class man, may not properly have a member function declared pure virtual higher up the hierarchy. eg. WagTail here class mammal{ public: virtual void WagTail() ...
0
votes
1answer
180 views

Should I always call super in Java? [duplicate]

Anti-pattern considerations aside, should I always call super in Java when overriding methods, even when the method is empty? Take a look at the code below, this is an excerpt from AsyncTask and can ...
1
vote
0answers
128 views

How to handle custom logic for multiple customers

We got a project where we have to process certain requests which the customer sends. The request is either in a generic format or in a customer format (all xml) which is then converted to the generic ...
0
votes
5answers
207 views

classes/inheritance, how do I handle special cases that require more change

I often find myself in this situation where I have a base class that does a lot of graphics. There are, for example, three strings that are positioned top, middle, bottom of an element. Like a scale ...
0
votes
2answers
118 views

Should I extend an interface or keep it independent?

I'm trying to decide whether to extend a base interface with additional methods or create several independent interfaces. I have a Task interface that looks like this interface Task { public ...
36
votes
11answers
4k views

Constructor-only subclasses: Is this an anti-pattern?

I was having a discussion with a co-worker, and we ended up having conflicting intuitions about the purpose of subclassing. My intuition is that if a primary function of a subclass is to express a ...
3
votes
1answer
158 views

How to design similar purpose static methods

I know that you cannot override static functions, but for what I'm looking to do, it seems like the logical way to design my class. I have a method that is associated with my abstract class, that i ...
0
votes
1answer
61 views

Implementing an interface already implemented by a parent class?

If a class extends a parent which defines an interface, should that class also explicitly implement the interface? As an example, is it better to include or remove the portion in square brackets ...
0
votes
1answer
73 views

refactor by inheritance or composition for large class that depend on common state [duplicate]

I have a huge class (>1000 lines), in which all methods depend on a small set of attributes in that class. I can think of 2 quick ways of refactoring. I keep it as a huge object, but split related ...
0
votes
3answers
83 views

Heuristics on when should one subclass and when not [closed]

Are there any good tips, when one should subclass and when should not? It's clear that we should not subclass when we want to reuse a single method of some class we override all methods, changing ...
0
votes
1answer
129 views

Class Table Inheritance: do child tables need a primary key?

I'm using the Class Table Inheritance architecture with my application and have read Fowler. When mapping my database tables to classes in code, I've realized I have no need for an "ID" property on ...
1
vote
3answers
182 views

Does overriding a method affect a superclass's call?

I'm trying to understand some of the nuances of inheritance but I can't find an answer to this question. Consider the following: class SuperClass { method foo { print "in SuperClass.foo" ...
2
votes
1answer
425 views

The Better Parts and Delegation: Is Crockford Too Extreme?

Douglas Crockford has recently been giving a talk called The Better Parts. The gist of the talk is that JavaScript developers should actually avoid a superset of the The Bad Parts, which now includes ...
1
vote
1answer
63 views

Compose nested EventEmitters elegantly

I am developing a server-side JavaScript tool, and trying to do so in a modular way. I have a Parser class that is an EventEmitter - you call parse and it will eventually emit data and end events. It ...
15
votes
8answers
4k views

Is it good practice to inherit from generic types?

Is it better to use List<string> in type annotations or StringList where StringList class StringList : List<String> { /* no further code!*/ } I ran into several of these in Irony.
1
vote
0answers
73 views

How does one enforce the implementation of a static method? [duplicate]

Of course the answer to the question above is, "you can't". But I didn't have a better way of describing what I'm looking for. If I did, I wouldn't be asking this question. My problem stems from a C# ...
3
votes
2answers
99 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
votes
2answers
92 views

what is the difference between inclusion and inheritance [duplicate]

the question is self explanatory, I just want to know when to use the inclusion and when to use inheritance, and which one serves for re-usability. in other words, which one meets the Object ...
2
votes
5answers
950 views

Is there an alternative to instanceof when filtering a Java stream by class?

I have an unexpected situation in a project in which all types extending one class are packed into a Java collection; but only a specific extension of that class contains an additional method. Let's ...
2
votes
1answer
431 views

Inheritance, Composition and Protocols

I'm reading a book on general object oriented concepts to try and gain a solid understanding of everything. One thing that is throwing me off is the difference between Inheritance and Composition. I ...
2
votes
0answers
87 views

Can/should objects share methods in certain circumstances?

I am currently working on a web application that is supposed to resemble an operating system with GUI (it manages processes and windows). I have several constructors which deal with different aspects ...
2
votes
2answers
65 views

How to ORM non-disjoint specializations

Consider this highly simplified sample of relational data (each table may be involved in one-to-many and many-to-many relationships not shown here): people +-------+------+--------+ | name | born | ...
0
votes
4answers
1k views

Why 'List<E>' is an 'interface' but not 'abstract class'?

Amidst defining the hierarchy, firstly, one can think to embed the abstract method(behavior) in abstract class only because the derive concrete class possess that behavior as core behavior with it's ...
2
votes
1answer
152 views

Should I use composition or inheritance for sharing configuration items?

I'm working on a fairly new project where I'd like to share some config items used by a JavaScript plugin. Specifically, the project is using jQuery dataTables and I want to find a good way of sharing ...
1
vote
1answer
83 views

Would this be considered a plugin or template type architecture?

I would like to build a system that basically offers the option to make various APIs interchangeable for usage to the end user. For example, the person using the software would have the option of ...
2
votes
3answers
308 views

Inheritance in imperative programming

My first introduction to programming was Java, which was horrible so I learnt Actionscript3 which was nice... "The point is" OOP was my introduction to programming where by I went on to gain a career ...
2
votes
4answers
228 views

Determining a class structure

I'm currently in the early stages of designing an e-commerce application. I'm working out some designs for my classes which have so far been fairly straightforward, but I've run into something of a ...
1
vote
3answers
268 views

Implements > extends, but what about variables?

It's preferable to write programs that depend on interfaces rather than on superclasses, but what when you want a class to have certain variables? Sometimes you want a class to implement a certain ...