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


  • 0

    Question: What is the difference between inheritance and composition?

    Answer:

    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


Log in to reply
 

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