Referring to the below link:
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2
The composition approach to code reuse pro
The key word in the sentence is "interface".
You'll almost always need to change the Apple
class in some way to accomodate the new return type of Fruit.peel
, but you don't need to change its public interface if you use composition rather than inheritance.
If Apple
is a Fruit
(ie, inheritance) then any change to the public interface of Fruit
necessitates a change to the public interface of Apple
too. If Apple
has a Fruit
(ie, composition) then you get to decide how to accomodate any changes to the Fruit
class; you're not forced to change your public interface if you don't want to.