Difference between Inheritance and Composition

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

    In Simple Word Aggregation means Has A Relationship ..

    Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition. Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.

    Why Use Aggregation

    Code Reusability

    When Use Aggregation

    Code reuse is also best achieved by aggregation when there is no is a Relation ship

    Inheritance

    Inheritance is a Parent Child Relationship Inheritance Means Is A RelationShip

    Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

    Using inheritance in Java 1 Code Reusability. 2 Add Extra Feature in Child Class as well as Method Overriding (so runtime polymorphism can be achieved).

提交回复
热议问题