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
27 views

Java redeclare inherited Interfaces?

While working on a small class called FractionNumber I found asking myself if I should implement an interface that I am already implementing. Sounds stupid I know but bear with me. My class ...
0
votes
1answer
69 views

Abstract class vs relation between classes

Searching for simplest solution where a User can order an item. If someone purchases an item he will have one address. Which then is the invoice and shipping address. But he could add another address ...
2
votes
3answers
102 views

Base Class vs Common Member Class

Imagine I have two classes like this... public class SingleplayerGameSetup { public int Seed { get; private set; } public int AiNations { get; private set; } } public class ...
4
votes
5answers
384 views

Is “too much methods” a valid reason to break the “composition over inheritance” rule?

I usually prefer to follow "composition over inheritance" rule, until I start stepping in the world of Java GUI. In my experience, for adding custom requirements in a GUI components, for example, add ...
1
vote
2answers
53 views

Refactoring to an inheritance hierarchy early on

I am building a management tool for recipes. Recipes have a bunch of data, including very generic stuff like an ID, tags, ratings and trivia. I currently only manage recipes, but i want to add support ...
1
vote
1answer
45 views

Can composition be used in unit/integration testing over inheritance

In larger projects, there can often be a group of tests that test the functionality of related classes. These tests often share a lot of setup and preparation functionality. What happens then is ...
2
votes
2answers
231 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 (...
1
vote
1answer
139 views

Inheritance is better or composition design pattern in this scenario?

Design and implement Cash Register: Given a number of items you will be required to calculate the total bill. Items are charged for in a couple of different ways: A given price for each item, e.g. ...
0
votes
1answer
52 views

Should I always use prefix private methods with an underscore in Python?

Prefixing methods and members with an underscore indicates internal use. For simple classes, I sometimes find the easier reading and typing of self.foo outweighing the indent of self._foo. Especially ...
9
votes
4answers
505 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,...
2
votes
0answers
56 views

Adding behaviour to a part of a third party class

I am working with an effectively third-party library. This library has a base class that's used as the interface (C++ style), ComponentBase. ComponentBase has a number of methods and setters that are ...
2
votes
1answer
107 views

Designing generic operations in object oriented languages

I found an interesting quote in SICP that I think is highly relevant in object oriented design: We see that, in general, a type may have more than one subtype. Triangles and quadrilaterals, for ...
0
votes
0answers
45 views

Code Duplication After Dependency Injection Instead Of Inheritance

I'm applying Dependency Injection instead of Inheritance. But now, I've the problem of code duplication. Main Controller final class PreferenceController implements PreferenceInterface { ...
-3
votes
1answer
302 views

What is disinheritance in object oriented programming? [closed]

I am confused over Virtual Inheritance and disinheritance ? Is both are same ?
101
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 ...
0
votes
0answers
25 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
500 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
127 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
635 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
230 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
94 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
114 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
433 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
217 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
85 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
135 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
206 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
109 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
145 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
97 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
198 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
330 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
8k 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 ...
7
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
230 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
100 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
47 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
76 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?