I’ve heard people saying that good design involves using inheritance instead of littering your code with if block. In what situation should we use inheritance, and when condition block is just fine?
I’ve heard people saying that good design involves using inheritance instead of littering your
Share
Generally, it isn’t good object-oriented programming practice to have code like the following:
The above can utilize polymorphism to call some method that will perform the action that is abstracted away in related classes with a common ancestor:
However, this practice definitely should not apply for all conditionals, however, when it comes to long
switchblocks orif-elseif-elseblocks, in many cases, use of inheritance will make your code more extensible and more robust.