Composition, I don't quite get this?

后端 未结 5 990
面向向阳花
面向向阳花 2021-01-21 04:27

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

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 04:46

    Return type of Fruit.peel() is being changed from int to Peel. This doesn't meant that the return type of Apple.peel() is being forced to change to Peel as well. In case of inheritance, it is forced and any client using Apple has to be changed. In case of composition, Apple.peel() still returns an integer, by calling the Peel.getPeelCount() getter and hence the client need not be changed and hence Apple's interface is not changed ( or being forced to be changed)

提交回复
热议问题