Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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, ...
wasatz's user avatar
  • 3,515
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 ...
Simon B's user avatar
  • 9,633
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 ...
Erik Eidt's user avatar
  • 34.1k
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 ...
Nathaniel Pisarski's user avatar
80 votes
Accepted

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

UPDATE: I've revised this answer. A number of good points were raised in the comments that deserved calling out. If my class implements an interface then can I say that I'm following inheritance? ...
Eric Lippert's user avatar
  • 46.2k
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 ...
JacquesB's user avatar
  • 59.7k
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 ...
Becuzz's user avatar
  • 4,835
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 ...
candied_orange's user avatar
51 votes

When should I extend a Java Swing class?

It's completely terrible in every possible way. At most, use a factory function to produce JButtons. You should only inherit from them if you have some serious extension needs.
DeadMG's user avatar
  • 36.9k
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 ...
Robert Bräutigam's user avatar
47 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 ...
candied_orange's user avatar
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 ...
IMSoP's user avatar
  • 5,857
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 ...
Alexander's user avatar
  • 4,904
36 votes

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

You want to avoid base classes knowing about derived classes. It introduces tight coupling and is a maintenance headache because you have to remember to add to the list every time you create a new ...
17 of 26's user avatar
  • 4,839
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 ...
Telastyn's user avatar
  • 109k
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'...
Pete's user avatar
  • 3,221
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 ...
amon's user avatar
  • 134k
32 votes
Accepted

Are Python mixins an anti-pattern?

Mixins just aren't a use case that was considered by the tool. That doesn't mean it's necessarily a bad use case, just an uncommon one for python. Whether mixins are used appropriately in a ...
Karl Bielefeldt's user avatar
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 ...
Theraot's user avatar
  • 9,151
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 ...
candied_orange's user avatar
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 ...
JimmyJames's user avatar
  • 27.5k
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'...
Doc Brown's user avatar
  • 208k
27 votes

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

Inheritance means writing a new subclass for a superclass. Writing a new class against an interface is implementing that interface. (And writing a new interface based on an old one is extending that ...
Kilian Foth's user avatar
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 ...
Greg Burghardt's user avatar
24 votes
Accepted

When should I extend a Java Swing class?

This violates the Liskov Substitution Principle because an OkayButton cannot be substituted in any place a Button is expected. For example, you can change the label of any button as you like. But ...
usr's user avatar
  • 2,764
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 ...
Jörg W Mittag's user avatar
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 ...
UltraBird's user avatar
  • 331
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 ...
Erik Eidt's user avatar
  • 34.1k
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 ...
Christophe's user avatar
  • 77.9k
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 ...
Michael Borgwardt's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible