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

2
votes
1answer
69 views

Child class accessing its parent's method from Ancestor method

I find myself right now banging my head with the following issue (in PHP): I have an abstract base class, which has a non-abstract method, inherited and unchanged all over the inheritance chain ...
-3
votes
0answers
39 views

Is it possible to access protected members of an object from the function of an object whose type is derived from type of first object?

I am aware that access-specifiers are class-based but I want to know if it extends to Inheritance also? Class base { protected: int x; } Class derived: public base { public: void f(base* pb){ ...
5
votes
1answer
83 views

How do I more effectively design this per its intended design while ensuring I'm meeting SOLID design principles?

I know what SRP is, but am questioning my current design of an object I call an entity. Below is a picture of the design I am referring to. If I shift the code from the GeometricInformation and ...
1
vote
1answer
68 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 ...
1
vote
2answers
132 views

Cleanest way to expand a base class without explicitly mapping properties in C#

Lets assume I have following base class: public class Base { public int Id {get; set;} public string SomeText {get; set;} public string SomeOtherText {get; set;} public static Base ...
2
votes
2answers
190 views

Do common MVC frameworks violate the LSP and is there a MVC framework which does not?

You who have worked with a framework implementing the MVC architectural pattern most likely know how these frameworks are usually implemented. They contain a base Controller class, which you extend, ...
2
votes
0answers
192 views

Is it a good practice to use virtual inheritance as default? [closed]

Why virtual inheritance is not used as default? Default as in by the programmer and not the language. If not, why? What are the cases where it may fail? Is there some run time overhead? Is it ...
8
votes
4answers
370 views

Parallel hierarchies - partly same, partly different

There are quite a few similar questions out there 1, 2, 3, 4, but non seems exactly the case in this question, nor do the solutions seem optimal. This is a general OOP question, assuming ...
4
votes
1answer
83 views

Is Python's inheritance an “is-a” style of inheritance or a compositional style?

Given that Python allows for multiple inheritance, what does idiomatic inheritance in Python look like? In languages with single inheritance, like Java, inheritance would be used when you could say ...
2
votes
2answers
196 views

What is meant by reuse of inheritance?

A 2013 study of 93 open source Java programs (of varying size) found that While there is [no] huge opportunity to replace inheritance with composition (...), the opportunity is significant ...
2
votes
2answers
231 views

How much implementation should I provide in an abstract class? [closed]

I have an abstract class AbstractService and about 10 classes that extend from AbstractService. However, I'm not sure how many methods and what methods I should implement in the abstract class and how ...
-1
votes
1answer
116 views

Inheritance hierarchy design approach - Javascript [closed]

For the below code, that creates inheritance hierarchy, function Animal(){ this.name = "Animal"; // toString is a function in the main Object that every // object inherits from ...
-4
votes
1answer
70 views

What actually happens in inheritance (java)? [closed]

class A { int a; int b; A(int i,int z) { a = i; } void abc() { System.out.println("a is " + a); System.out.println("b is " + b); } } class B ...
36
votes
5answers
2k 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 ...
0
votes
2answers
109 views

Best practice for encapsulating a parameter that requires multiple interfaces to be implemented

I encounter this issue multiple times, but I'm never sure how best to deal with it. Basically, some methods I write require the implementing object to support multiple interfaces. The example I have ...
2
votes
3answers
88 views

Is there particular circumstance that throwing root superclass exceptions is a good practice?

I've been taught that exceptions should be have concise meanings and should contain a message that explains to the client what the exceptional situation is. I am wondering, since I found a piece of ...
0
votes
3answers
251 views

Using super keyword to access super class members

I have following questions about the super keyword in java: I have a code below with classes: A Extends no class contains integer variable i B Extends class A contains integer variable i C Extends ...
7
votes
5answers
442 views

Refactoring three very similar classes using inheritance?

I'm currently working on refactoring the code-base for one of our services. I'm been going through reviewing everything, and I feel it's a bit scattered, and could probably adhere to OOP principles ...
4
votes
3answers
277 views

Composition over inheritance but

I'm trying to teach myself software engineering and coming up against some conflicting information which is confusing me. I've been learning OOP and what abstract classes / Interfaces are and how to ...
17
votes
1answer
2k views

Why not make a language with mixin-only inheritance? [duplicate]

It seems that in all class-based or prototypal OOP languages, mixins are either an afterthought or a secondary feature. However, to me it looks like traditional inheritance is just a specific case of ...
0
votes
2answers
335 views

Am I overusing Inheritance or this is exactly what it is here for?

I have a class called BagItem and another class called EquipmentItem both classes are exact the same in methods, etc, with the exception of 1 property. BagItem have: public InventoryBagType BagType ...
2
votes
4answers
163 views

Is it a good idea to make method behavior depend on the calling thread?

I want to subclass a 3rd party class, in order to make it thread-safe. I have a good idea of how to implement this, but there is a problem: the superclass has a property, which affects the behaviour ...
1
vote
1answer
120 views

Is this the preferred way to do inheritance in JavaScript?

Hey there I'm not new to programming nor javascript but I'm new to the object oriented world of javascript as it differs from languages I know such as java, c# when it comes to object oriented ...
1
vote
2answers
202 views

To god (class) or not to god? [duplicate]

I'm working on a project that requires several different users in the system, all of which have overlapping responsibilities; we've isolated two possible ways of tackling this problem, but we're ...
0
votes
2answers
166 views

What is the pattern that uses multiple instances rather than multiple classes called? When would I use it?

Sometimes I find it useful to have a single class with multiple instances (configured differently via their properties), rather than multiple classes (inheritance). ??? Pattern Single class (Fruit) ...
0
votes
1answer
70 views

Subclassing vs Using properties to discriminate types

Often when writing code I wonder what the best way is to discriminate between types (by which I don't necessarily mean types used in typesystems). That brings up 3 questions: What is better? When is ...
18
votes
1answer
489 views

Why does(/did) Bertrand Meyer think subclassing is the only way to extend a “closed” module?

In Meyer's Object-Oriented Software Construction (1988) he defines the open/closed principle as follows: A module will be said to be open if it is still available for extension. For example, it ...
2
votes
2answers
307 views

Python OO problem

I started learning Python yesterday and I ran into a problem. I like to hear some thoughts on it. As an exercise, I decided to build a chatserver. As part of the exercise, I wanted to write some ...
5
votes
8answers
299 views

Designing around shallow constness with inheritance

Background I'm writing an image handling class. For this question two requirements of the class are of interest: Must have "deep" const correctness. Must allow sub-image aliases, a.k.a. sections or ...
1
vote
2answers
444 views

How to solve this inheritance problem

I have the following classes: public abstract class StaticFileController<File, QueryData> : AsyncController { private string _resourceName; public StaticFileController(string ...
-3
votes
1answer
59 views

How can we access non static protected method in main{as we cannot access protected members by creating objects} for java? [closed]

package foo; public class scj{ protected void disp() { System.out.println("package"); } } ********************* import foo.scj; public class aq extends scj{ public static void main(String ...
3
votes
5answers
282 views

Inheritance when following the Repository Pattern in PHP

I am trying to build a PHP application using the Repository Pattern but I'm not sure how I should implement the save method. I have an abstract class called ItemRepository which have the following ...
3
votes
1answer
102 views

When are subclasses redundant?

I am working on a ML project that will involve malware and I want to represent the statistics files used for training the classifier via StatsFile objects. However, I am wondering whether I should use ...
0
votes
2answers
129 views

What exactly is interface compliance in C++?

I was reading this. It says that Some people believe that the purpose of inheritance is code reuse. In C++, this is wrong. Stated plainly, “inheritance is not for code reuse.” The purpose ...
1
vote
1answer
83 views

“Is a” relationship or, in other words Inheritance

Say I define an interface IAnimal which has a pure virtual (abstract) method called eat like this: class IAnimal { virtual void eat(Food*) = 0; }; In the future I inherit form IAnimal and create ...
1
vote
0answers
74 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 ...
1
vote
1answer
118 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
485 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
287 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
208 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 ...
11
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
43 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 { ... } ...
7
votes
4answers
321 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
176 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
120 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
74 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
82 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 ...
9
votes
5answers
387 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
532 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
236 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 ...