129
votes
Is "composition over inheritance" violating "dry principle"?
A common misunderstanding with the DRY principle is that it is somehow related to not repeating lines of code. The DRY principle is "Every piece of knowledge must have a single, unambiguous, ...
126
votes
Need Good OOP Design For World and Countries Problem
I wouldn't use any form of inheritance for World and Country. The World is not a Country, and a Country is not the World.
Instead, the World is a "bag" which contains many Countries. By "bag" I ...
119
votes
Accepted
Why is inheritance bad in a Person-Student model?
The problem I have with this model is that teacher & student are roles while person is a real entity. While this model will work in the short term, it will have problems if: a student becomes a ...
90
votes
Accepted
Polymorphism case study - Design pattern for morphing?
I know that this is meant to be a fairly contrived example to demonstrate the idea at play. But I would say that as a rule of thumb, you want to exhaust every possible option before relying on Subtype ...
69
votes
Is "Parent x=new Child();" instead of "Child x=new Child();" a bad practice if we can use the latter one?
It depends on the context, but I would argue you should declare the most abstract type possible. That way your code will be as general as possible and not depend on irrelevant details.
An example ...
65
votes
Why inherit a class without adding properties?
This is something that I use to prevent polymorphism from being used.
Say you have 15 different classes that have NamedEntity as a base class somewhere in their inheritance chain and you are writing ...
53
votes
Is it logical to not use inheritance because the function is critical?
This is a “protect the idiots” rule. I’ve seen it in many forms. Once entrenched it’s very difficult to overturn. I’ve seen rules like this forbid code that is identical to code in the language ...
51
votes
Is it ok to inherit a class without adding anything to the child, to respect the Open Closed principle?
No. Emphatic no.
Unless I misunderstood you, the question is to subclass for a different behavior, but actually not have the behavior itself. Instead an outside actor checks the exact type and does ...
48
votes
Accepted
Is "composition over inheritance" violating "dry principle"?
Er wait you're concerned that repeating
public LoginPage loginPage;
in two places violates DRY? By that logic
int x;
can now only ever exist in one object in the entire code base. Bleh.
DRY is a ...
44
votes
Why can't a mutable interface/class inherit from an immutable one?
Adding mutability may be unwise in general, but it's making a subclass do something the parent cannot, which is how OOP is supposed to work, no?
A key concept in OOP is that of substitutability: if I ...
37
votes
Why is it necessary to mark classes as not inherited from? Can't an optimizer automatically detect that virtual calls are unnecessary?
Don't we need to take a step back here?
Under the hood, it generally all boils down to simply functions being called the with the this pointer as first arg.
It's good to question things from first ...
35
votes
Accepted
What is a type system?
That all seems like a fine description of what type systems provide. And your implementation sounds like a reasonable enough one for what it's doing.
For some languages, you won't need the runtime ...
34
votes
Accepted
When to move a common field into a base class?
It all depends upon the exact problem you're trying to solve.
Consider a concrete example: your abstract base class is Vehicle and you currently have the concrete implementations Bicycle and Car. You'...
34
votes
Accepted
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
This is not an LSP violation, because the object is immutable and doesn't overload any instance methods in an incompatible way.
The Liskov Substitution Principle is fulfilled if any properties about ...
29
votes
Why the industry prefer/use composition over inheritance?
TL;DR
Attempting to use composition first, before attempting to use inheritance, prevents naive mistakes. Also, Class Inheritance:
Is easy to misuse.
Is less versatile than composition.
Is less ...
29
votes
Why inherit a class without adding properties?
This is my favorite use of inheritance. I use it mostly for exceptions that could use better, more specific, names
The usual issue of inheritance leading to long chains and causing the yo-yo ...
28
votes
"Prefer composition over inheritance" - Is the only reason to defend against signature changes?
I like analogies so here's one: Have you ever seen one of those TVs that had a VCR player built-in? How about the one with a VCR and a DVD player? Or one that have a Blu-ray, DVD and VCR. Oh and ...
28
votes
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
On a first glance, it looks like the LSP will be violated, since replacing an int object by an PositiveInteger in a function which expects just an int gives the impression it could break the function'...
26
votes
Is it logical to not use inheritance because the function is critical?
This is difficult to tell without analyzing the code. I can't provide you an authoritative answer, but perhaps some guidance on how to make this decision will do.
In a situation like this, the first ...
24
votes
Why can't a mutable interface/class inherit from an immutable one?
My understanding of Inheritance in Object-Oriented Programming is that you use it to add behaviors/methods, such as set methods.
A lot of mainstream object-oriented languages, and Java and Kotlin are ...
23
votes
Are Python mixins an anti-pattern?
I believe that Mixins can, absolutely, be Pythonic. However, the idiomatic way to silence your linter--and improve your Mixins' readability--is to both (1) define abstract methods that explicitly ...
23
votes
What is a type system?
I like @Telastyn's answer especially because of the reference to academic interest in formalism.
Allow me to add to the discussion.
What is a type system?
A type system is a mechanism for ...
22
votes
Polymorphism case study - Design pattern for morphing?
The person, whether married or not is still the same person. Morphing it to another kind of person with a copy replace approach does not maintain this fundamental identity.
Several other ...
21
votes
Accepted
Are protected properties evil?
I don't think duplicating the property in the subclass (and its constructor) is really a better alternative. For actual read/write properties, that can't even yield the same functionality, and for ...
19
votes
Why should I prefer composition over inheritance?
You will see this cycle a lot in software development discourse:
Some feature or pattern (let's call it "Pattern X") is discovered
to be useful for some particular purpose. Blog posts are written ...
19
votes
Need Good OOP Design For World and Countries Problem
Here is a thought or two:
Are you sure you even need to model the World? Based on your description it doesn't seem to have any effect. Yes it encapsulates your Countries, but if thats all the program ...
18
votes
Is it ok to have an empty abstract class to make concrete classes polymorphic
public interface IDockable<T>
{
public void Dock(T config);
}
By having a type parameter, you can specify the parameter you want to accept within the Dock method in your classes. Example:
...
18
votes
What is a type system?
Oh man, I am excited to try to answer this question as best I can. I hope I can get my thoughts properly in order.
As @Doval mentioned and the questioner pointed out (albeit rudely), you do not ...
18
votes
Accepted
How to implement RealNumber and ComplexNumber inheritance?
Even if in a mathematical sense, a real number is a complex number, it is not a good idea to derive real from complex. It violates the Liskov Substitution Principle saying (among other things) that a ...
17
votes
Are Python mixins an anti-pattern?
The linter is not aware that you use a class as a mixin. Pylint is aware that you use a mixin if you add the suffix 'mixin' or 'Mixin' at the end of the class name, then the linter stops complaining.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
inheritance × 582object-oriented × 190
java × 99
object-oriented-design × 91
c# × 84
design-patterns × 77
composition × 71
interfaces × 69
c++ × 61
design × 53
polymorphism × 32
python × 30
class-design × 23
multiple-inheritance × 22
abstract-class × 21
liskov-substitution × 21
javascript × 19
php × 16
solid × 16
refactoring × 15
class × 14
abstraction × 13
constructors × 11
architecture × 10
programming-languages × 10