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 ...
                
            
       
        
            
                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 ...
                
            
       
        
            
                54
            
            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 ...
                
            
       
        
            
                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 ...
                
            
       
        
            
                30
            
            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
        
        
            
            
        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'...
                
            
       
        
            
                27
            
            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 ...
                
            
       
        
            
                22
            
            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 ...
                
            
       
        
            
                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 ...
                
            
       
        
            
                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
        
            
                
                Accepted
            
        
            
            
        Why does the base class need to have a virtual destructor here if the derived class allocates no raw dynamic memory?
                    When the compiler goes to execute the implicit delete _ptr; inside of the unique_ptr's destructor (where _ptr is the pointer stored in the unique_ptr), it knows precisely two things:
The address of ...
                
            
       
        
            
                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.
...
                
            
       
        
            
                17
            
            votes
        
        
        Coding to Interfaces vs Abstract Inheritance
                    Am I misunderstanding the concept of "code to an interface and do not use inheritance"?
Yes you are misunderstanding that, because you seem to be conflating two different principles.
On the one hand, ...
                
            
       
        
            
                17
            
            votes
        
            
                
                Accepted
            
        
            
            
        Why inherit a class without adding properties?
                    For me, this makes sense as it gives a concrete name to the class, instead of relying on the generic NamedEntity. On the other hand, there is a number of such classes that simply have no additional ...
                
            
       
        
            
                17
            
            votes
        
        
            
        Is it logical to not use inheritance because the function is critical?
                    it's important for that function to be correct, so we want to ensure that the developer of a new sub-class goes through the steps to make sure they have the right logic.
I would question the core ...
                
            
       
        
            
                16
            
            votes
        
            
                
                Accepted
            
        
            
        Is it ever okay to violate the LSP?
                    The LSP is concerned about subtyping and polymorphism. Not all code actually uses these features, in which case the LSP is irrelevant. Two common use cases of inheritance language constructs that are ...
                
            
       
        
            
                16
            
            votes
        
            
                
                Accepted
            
        
            
            
        C# design to force the virtual method to be called from the override, or something similar
                    Because same effect could already be achieved by a standard pattern of empty overridables.
If all of your non-sealed methods have empty body, it would not matter if they are called from overridden ...
                
            
       
        
            
                15
            
            votes
        
            
                
                Accepted
            
        
            
            
        Why were default methods introduced to Java?
                    The reason that default methods were introduced was that without them, it's impossible to add new methods to an interface and maintain backwards compatibility.  As explained here:
Default methods ...
                
            
       
        
            
                14
            
            votes
        
        
            
            
        Composition over inheritance when adding functionality to a foreign object
                    Composition over inheritance
[A] Composition over inheritance is generally a good rule to follow,
[B] but there are some cases where inheritance is a must
Your conclusion in B implies that you are ...
                
            
       
        Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
inheritance × 585object-oriented × 191
java × 99
object-oriented-design × 91
c# × 85
design-patterns × 78
composition × 71
interfaces × 69
c++ × 62
design × 53
polymorphism × 32
python × 30
class-design × 23
multiple-inheritance × 22
abstract-class × 21
liskov-substitution × 21
javascript × 19
solid × 17
php × 16
refactoring × 15
class × 14
abstraction × 13
constructors × 11
architecture × 10
programming-languages × 10
 
         
         
         
         
         
         
         
         
         
         
         
        