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

-3
votes
1answer
280 views

What is disinheritance in object oriented programming? [on hold]

I am confused over Virtual Inheritance and disinheritance ? Is both are same ?
100
votes
9answers
16k views

Is there any “real” reason multiple inheritance is hated?

I've always liked the idea of having multiple inheritance supported in a language. Most often though it's intentionally forgone, and the supposed "replacement" is interfaces. Interfaces simply do ...
52
votes
17answers
6k views

Is OO-programming really as important as hiring companies place it? [closed]

I am just finishing my masters degree (in computing) and applying for jobs. I've noticed many companies specifically ask for an understanding of object orientation. Popular interview questions are ...
2
votes
2answers
201 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 (...
0
votes
0answers
16 views

Abilities interacting with aspects of other abilities - how to implement efficiently

In my game I want characters to be able to cast spells which deal a certain amount of damage of a certain type to other characters in the game. I also want abilities that negate an amount of damage ...
1
vote
3answers
7k views

What is good practice when inheriting static classes isn't possible/allowed

I am developing a project in C# and due to a design decision it is not possible to have a static class inherit another static class. But I have, in my opinion, a case where this would make sense. I ...
4
votes
1answer
477 views

Should I declare the most specific type as return type of method?

In Android I can put only ArrayList of Parcelable objects in Intent: Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value); I can't put in there a List<? ...
15
votes
3answers
2k views

Are there any OO languages without inheritance?

During a code review today, a colleague of mine said something interesting: prototype is only useful when you need inheritance - and when's inheritance ever a good idea? I thought about this and ...
5
votes
1answer
114 views

Inheritance from children to parents?

Seeing this UML class diagram : FileHandler, Uploader and Deleter are abstract classes. The four bottom classes are implementations. They extend on FileHandlerInterface which requires a handle ...
1
vote
2answers
613 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) , ...
17
votes
5answers
3k views

When to use inheritance, when to use 'just a boolean field'?

In our Rails application, we are adding notifications. Some of these are blocking: They stop the progress of whatever resource they are added on, because some information on that resource is missing. ...
6
votes
2answers
226 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 ...
3
votes
2answers
87 views

Creating Instances from same SuperClass Object

I have 3 subclasses (LowState, MediumState, HighState) which derive from the same superclass (State). Within the superclass, State, I have declared several variables which need to be accessed and ...
86
votes
12answers
11k 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 ...
4
votes
2answers
110 views

Applying composition over inheritance to Vehicle classes

I have a car maintenance garage program that has an abstract Vehicle class that has several derived classes like Car MotorCycle, etc. Each of those derived vehicles in turn is either a fuel or an ...
35
votes
5answers
3k views

When should I extend a Java Swing class?

My current understanding of Inheritance implementation is that one should only extend a class if an IS-A relation is present. If the parent class can further have more specific child types with ...
2
votes
2answers
31 views

Refactor using CTI or create new Entities

I'm currently developing on a rather large e-commerce application that handles multiple different business-fields. Like every e-commerce application we have a Concept called an Order which has one or ...
8
votes
5answers
410 views

Inheritance vs additional property with null value

For classes with optional fields, is it better to use inheritance or a nullable property? Consider this example: class Book { private String name; } class BookWithColor extends Book { private ...
5
votes
3answers
216 views

Is it ever appropriate to use subtyping for a has-a relationship?

One of the basic precepts of object oriented programming is that subtyping represents the "is-a" relationship. That is, the child is always a specific form of the parent. A common example is that a ...
27
votes
3answers
5k views

If I implement an Interface, is it called an Inheritance?

If my class implements an interface then can I say that I'm following inheritance? I know that when a class extends another class then it's inheritance.
1
vote
1answer
80 views

Get or infer template type from inherited member

So I have an assignment from college where I have to apply multiple metaheuristics to different problems. I thought that I should make everything as modular and reusable as possible to reuse ...
1
vote
2answers
133 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
5answers
202 views

What is the correct OOP relation between complex and real numbers?

On one hand class complex { double real; double imaginary; } it is composition on the other hand class complex : double { double imaginary; } it is inheritance Which one is ...
16
votes
3answers
2k views

Is there a specific name for the “Square inherits from Rectangle” paradox?

A certain failure of OOP is shown with a class Square inheriting from Rectangle, where logically Square is a specialization of Rectangle and should therefore inherit from it, but everything falls ...
-1
votes
2answers
107 views

Do you say “this” on protected members? [closed]

Background I have never had a formal class in OOP/OOD although I understand how the protected members work. Being accessible by any instance/derived class, they are thereby less private (more ...
2
votes
1answer
144 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
96 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 ...
3
votes
2answers
197 views

How do I deal with unneeded inherited members?

Consider the following UML diagram: In a nutshell: ChildClass1 and ChildClass2 use all of the members of ParentClass. ChildClass3only uses Member1 and Member2 and the value of Member5 is a ...
4
votes
1answer
319 views

Liskov substitution principle with abstract parent class

Does Liskov substitution principle apply to inheritance hierarchies where the parent is an abstract class the same way if the parent is a concrete class? The Wikipedia page list several conditions ...
6
votes
5answers
1k views

Does this code solve the square/rectangle Liskov Substution Principle example?

I just wanted to check that I understand the LSP correctly and can solve it. I am taking the classic rectangle/square problem and attempting a solution: class Rectangle{ public $width; ...
13
votes
1answer
1k views

How to verify the Liskov substitution principle in an inheritance hierarchy?

Inspired by this answer: Liskov Substitution Principle requires that Preconditions cannot be strengthened in a subtype. Postconditions cannot be weakened in a subtype. Invariants ...
46
votes
9answers
7k views

Code Smell: Inheritance Abuse [duplicate]

It's been generally accepted in the OO community that one should "favor composition over inheritance". On the other hand, inheritance does provide both polymorphism and a straightforward, terse way of ...
6
votes
2answers
2k views

How does strengthening of pre conditions and weakening of post conditions violate Liskov Substitution principle?

I read that Liskov substitution principle is violated if : Pre conditions are strengthened . Post conditions are eased out. But I don't get fully yet how these two points would violate Liskov ...
33
votes
6answers
9k 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 ...
2
votes
2answers
226 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, ...
0
votes
2answers
80 views

Why is the _replace method of Python namedtuple classes protected?

In the section Method Names and Instance Variables, the Python Style Guide (aka PEP 0008) says, "Use one leading underscore only for non-public methods and instance variables." Why then, does the ...
1
vote
3answers
44 views

How to represent the following scenario in a class diagram?

Consider a store with multiple branches and a system generates reports for each branch monthly and annually and an overall report as well monthly and annually. So in total there are 4 types of ...
0
votes
1answer
70 views

How do you write super() in the UML format?Is it correct to write super(parameter: type): return type in the UML methods section?

Suppose you write a subclass that extends to a certain class and in that subclass, you use the super() method for your constructor. Would you write "super(parameter: type): return type" in the UML?
2
votes
3answers
90 views

Calling a constructor from a parent class in a derived class

I'm trying to create a parent class with a constructor that takes a single int as a parameter. I also need to derive a child class that creates two instances of the parent class using a constructor ...
3
votes
6answers
316 views

Can we say “If a parent class never appears in my codes except in its child class, it should be composition instead of inheritance”?

I read some posts about "composition over inheritance","where to use composition/inheritance" , "Is-a relationship..." or "Liskov substitution principle" for some time, but I am not sure if I get the ...
0
votes
2answers
199 views

Per my design requirements, does this design hierarchy seem reasonable?

Background Construction Note that I am using C# here, but it may not be necessary to provide input to my conceptual questions about design. Consider the following design methodology... I work at a ...
8
votes
7answers
370 views

A property that can represent both a single date and a date range: How to properly model that?

I work in a system that can represent a "shipping estimate" in two ways: A specific date: The item is guaranteed to ship at that date A day interval: The item will be shipped "X to Y" days from ...
9
votes
3answers
287 views

Are Python mixins an anti-pattern?

I'm fully aware that pylint and other static analysis tools are not all-knowing, and sometimes their advice must be disobeyed. (This applies for various classes of messages, not just conventions.) ...
0
votes
3answers
197 views

What to do if I need more than one base class in C#? [closed]

Let's say I have a grid with square fields. For the fields I have an abstract Field class. This class has several subclasses, for example EmptyField or RoadField. Some of these fields can be connected ...
2
votes
1answer
177 views

Creating multiple instances of an implementing class

I currently have an abstract class and multiple classes extending it. My problem is, that there has to be a way to create a variable number of instances of an extending class. The number of instances ...
1
vote
1answer
136 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 'Vote<T>.CalculateWinner(...
1
vote
1answer
111 views

Is this a misuse of “composition over inheritance”?

The general sentiment seems to be that composition is preferable, in most cases, over inheritance, as it leads to less coupling. But for a case like this, module Publishable def new? @content....
3
votes
2answers
457 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 ...
8
votes
4answers
461 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 polymorphism,...
5
votes
1answer
87 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 ...