When To Avoid Inheritance?
2016.06.18 - 2020.03.09
What Not To Do
Do not create parent class only for common methods
Do not create a parent class only to inherit common members (class variables)
Do not create a parent class if you find that you cannot add children
Do not create a class hierarchy if you got warnings
Do not create a parent class if you have to call super or pass this
super or pass thisDo not create a parent class if you find that it must be modified when a child class is added or modified
Do not create a parent class if you do not plan to use it as a declaration type
Do not create a parent class only to do something on an abstract level
Do not create an abstract parent class if you can remove its abstract methods and the program still compiles
Do not create abstract methods if they are neither public nor called in the abstract class
Do not create a parent class if it has no public interface
Do not create a parent class if it has no own behavior
Do not create a parent class when you cannot unit test its behavior
Do not create a parent class if you cannot find a good name for it
Do not create a parent class if it is not cohesive
Favor composition over inheritance
Use interface instead of base class if a common interface is really needed
Create polymorphic classes only if you can exactly name the design pattern with polymorphic classes you want to use
Do not create polymorphic classes if you cannot prove that it is really necessary
Do not create a parent class for a small benefit or obvious code
Do not create parent classes just because you will configure the specific class in the injection framework
Do not create abstract methods for non-procedural differences
Do not create child classes if they contain only non-procedural differences
Create a separate interface for the parent and child classes instead of inheritance
Do not use type-cast
Do not create an abstract framework on top of a framework
Never create parent class for test classes
What To Do When Using Inheritance
Make methods final
Parent and abstract classes must be unit tested too
What To Keep In Mind
Abstract classes and parent classes are classes
Parent and child classes make one polymorphic type
Inheritance is the strongest dependency
Occam's razor
Last updated