Explain inheritance using an “is-a” relationship and composition using a “has-a” relationship.

Like(1078 Likes)
  • For inheritance, you can use a relationship such as a Dog “is-a” Pet. 
    • This relationship can help clarify situations in which inheritance is not valid. 
      • For example, you would not say a Tail “is-a” Dog, thus these two classes should not be in an inheritance relationship.
  • For composition, you can use a relationship such as a Dog “has-a” Tail. 
    • This relationship can help clarify situations in which composition is not valid. 
      • For instance, you would not say a Dog “has-a” Pet, thus these two classes should not be in a composition relationship.