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
0answers
39 views

If the externally callable interface of a class is called its API, what is “what can be overridden” called?

It's about what the class gives access to through inheritance. It has a three letter abbreviation but not API. I've seen this before but can't remember at the moment. The term is usually used in ...
0
votes
0answers
41 views

Accepted Patterns For Base Class with both Static And User-Defined Data Sources

I have a requirement to build a 'base' profile library (in C# - NET 4.5.2) for various different internal (technical) consumers. This profile consists of metadata fields that would be shared by a ...
4
votes
3answers
245 views

How can I add properties to subclasses and access them without casting from a superclass?

I'm trying to model a multi-dimensional point class in C#. I have about eight different types of points, and there may be more in the future. Right now, I have a superclass (PointBase) that holds all ...
3
votes
3answers
242 views

“Correct” way to use inheritance

Within a small project, a class Storage is meant to store any type of Item. Now, an Item has a String name, and an interaction. For example, a clock item might implement the interaction increment(int ...
4
votes
2answers
98 views

C++ design for encoder/decoder classes with different stored types

I need to handle different elements in a vector, each element owning a specific parameter (integer or string), so that I can easily handle encoding/decoding of a series of elements. Encoding a list ...
10
votes
3answers
1k views

Is an interface considered 'empty' if it inherits from other interfaces?

Empty interfaces are generally consider bad practice, as far as I can tell - especially where things like attributes are supported by the language. However, is an interface considered 'empty' if it ...
1
vote
2answers
34 views

Declaring 'chained' interface inheritance

I assume this is largely language-agnostic, but I'm working in C# if it's not Let's say interface I3 inherits from I2, which in turn inherits from I1. I could write this: interface I1 { ... } ...
6
votes
4answers
280 views

Can I make my constructors less repetitive?

I'm extending a class with 10 different constructors. The new subclass, SpecialImage, is used like this: SpecialImage specialImage = new SpecialImage(..); // Leverage the Rotate() method of ...
1
vote
1answer
125 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 ...
1
vote
1answer
102 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 ...
2
votes
1answer
64 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
78 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
107 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 ...
10
votes
5answers
455 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
194 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
92 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
263 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
301 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
62 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
158 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
165 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 ...
31
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
52 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
211 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
166 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
47 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
200 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
230 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
184 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
136 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
212 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
125 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 ...
37
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
163 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
67 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
80 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
139 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
188 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
498 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
75 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
105 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
98 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 ...
3
votes
5answers
1k 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
479 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 ...