Composition, I don't quite get this?

后端 未结 5 994
面向向阳花
面向向阳花 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条回答
  •  旧时难觅i
    2021-01-21 04:38

    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.

提交回复
热议问题