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

-5
votes
0answers
15 views

Accessing and using parent class members with super Python [on hold]

The code below is not running and I guess I am a little confused as to how to use parent class members through child class functions. class First(object): #a=b=0 def __init__(self,a,b): ...
7
votes
5answers
289 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
209 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 ...
25
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
70 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 ...
3
votes
5answers
187 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 ...
-1
votes
2answers
95 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 ...
3
votes
2answers
193 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 ...
0
votes
2answers
60 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
32 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
65 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
83 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
311 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 ...
9
votes
3answers
252 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
191 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 ...
8
votes
7answers
341 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 ...
1
vote
1answer
105 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? ...
2
votes
1answer
151 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 ...
2
votes
1answer
141 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 ...
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 ...
1
vote
1answer
112 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 ...
3
votes
2answers
354 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
223 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
209 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
421 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
88 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
197 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
241 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
118 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
80 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
115 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
92 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
570 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 ...
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; ...
7
votes
5answers
461 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
364 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
339 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
168 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
127 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
208 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
167 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
73 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
508 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
319 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
306 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
484 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
70 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 ...