What is the difference between inheritance, composition, aggregation, and association?

  • 0

    Question: What is the difference between inheritance and composition?


    Composition is used to represent a HAS A relationship.
    Inheritance is used to represent a IS A relationship.

    For example, in Java:

    class Name {} // The name class.
    class Animal{} // Animal class which is parent to Dog class.
    // Dog IS AN Animal, so Dog class extends Animal class.
    class Dog extends Animal { 
     // Dog HAS A Name, so Dog class has an instance of Name class as its member.
     private Name name;

    Companies: Adobe

  • 0

    Just to add an additional comment that composition and aggregations are both UML associations e.g one class is connected with another. They describe strong and weak whole-part relationship .Composition defines a relationship where the lifecycle of the containing object (owner) and the contained object are related. When the owner is destroyed, then the contained object also is destroyed. Aggregation on the other hand doesn't require that lifecycle of the contained object depends on lifecycle on the containing object.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.