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

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 ...
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
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 ...
0
votes
2answers
512 views

block access to area/controller's methods but allow inherited controller to run methods, asp .net mvc5

I am trying to find out how to block a whole area/controller from being accessed by typing url website.com/area1/basecontroller/method1 (this area also contains the views that will be displayed) , ...
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 ...
14
votes
7answers
35k views

Why are constructors not inherited?

I am confused as to what the problems could be if a constructor was inherited from a base class. Cpp Primer Plus says, Constructors are different from other class methods in that they create ...
32
votes
6answers
8k views

LSP vs OCP / Liskov Substitution VS Open Close

I am trying to understand the SOLID principles of OOP and I've come to the conclusion that LSP and OCP have some similarities (if not to say more). the open/closed principle states "software ...
1
vote
4answers
420 views

Implements > extends, but what about variables?

It's preferable to write programs that depend on interfaces rather than on superclasses, but what if you want a class to have certain variables? Sometimes you want a class to implement a certain ...
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 ...
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 ...
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
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 ...
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 ...
-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 ...
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 ...
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 ...
1
vote
1answer
1k views

Changing method signature for implementing classes in PHP

Is there any decent work around to PHP's lack of Generics that allow static code inspection to detect type consistency? I have an abstract class, that I want to sub-class and also enforce that one of ...
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 ...
5
votes
6answers
3k 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
2answers
130 views

Should a parent CSS class be added to the markup or @extended?

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; ...
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 ...
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 ...
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 ...
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 ...
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 ...
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) ...
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 ...
5
votes
4answers
6k views

OOD: Java inheritance and access to child methods via casting

I have several classes Parent and Child1 ... Child9 implemented in Java. Parent is an abstract class, containing all the common variables of the child classes (a lot, which is the main reason why I ...
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 ...
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 ...
5
votes
1answer
452 views

Is using protected inheritance to hide (but exploit) implemented interface from the public ok?

Recently, we had a discussion about code using protected inheritance in order to hide the fact (to client code) a class inherits from a specific base class but to exploit this fact in the ...
0
votes
2answers
611 views

Can an object call a method on its 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 ...
-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 ...
15
votes
8answers
6k 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.
2
votes
5answers
2k views

How do inheritance and composition differ?

I'm wondering about the differences between inheritance and composition examined with concrete code relevant arguments. In particular my example was: Inheritance: class Do: def do(self): ...
47
votes
4answers
26k views

Why should I prefer composition over inheritance?

I always read that composition is to be preferred over inheritance. A blog post on unlike minds, for example, advocates using composition over inheritance, but I can't see how polymorphism is ...
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 ...
5
votes
1answer
394 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 ...
3
votes
1answer
245 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
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 ...