I think from my point of view it breaks in case of addition of new methods in base or super class. If A
is a subclass of B
, even if A
is not modified in any way, a change in B
can break A
. (This is called the Ripple Effect)
Example: Suppose A
overrides all methods in B
by first validating input arguments in each method (for security reasons). If a new method is added to B
and A
is not updated, the new inherited method would introduce a security hole.
To get rid of pitfalls in inheritance, favor or use “Composition” Instead of Inheritance, simple aggregation instead of inheritance , below is an example:
Imagine two classes, Person
and Employee
. Instead of asking Employee
to inherit from Person
, you can compose Person
inside Employee
and forward the requests for Person
functionality to the composed class So We still get the benefit of reusing Person
class.
Notes: