Difference between Inheritance and Composition

前端 未结 17 2403
忘了有多久
忘了有多久 2020-11-22 02:35

Are Composition and Inheritance the same? If I want to implement the composition pattern, how can I do that in Java?

17条回答
  •  清歌不尽
    2020-11-22 03:33

    Composition is where something is made up of distinct parts and it has a strong relationship with those parts. If the main part dies so do the others, they cannot have a life of their own. A rough example is the human body. Take out the heart and all the other parts die away.

    Inheritance is where you just take something that already exists and use it. There is no strong relationship. A person could inherit his fathers estate but he can do without it.

    I don't know Java so I cannot provide an example but I can provide an explanation of the concepts.

提交回复
热议问题