Difference between Inheritance and Composition

前端 未结 17 2316
忘了有多久
忘了有多久 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:27

    I think this example explains clearly the differences between inheritance and composition.

    In this exmple, the problem is solved using inheritance and composition. The author pays attention to the fact that ; in inheritance, a change in superclass might cause problems in derived class, that inherit it.

    There you can also see the difference in representation when you use a UML for inheritance or composition.

    http://www.javaworld.com/article/2076814/core-java/inheritance-versus-composition--which-one-should-you-choose-.html

提交回复
热议问题