Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 981935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:37:14+00:00 2026-05-16T04:37:14+00:00

I hear (and read on this site) a lot about "favour composition over inheritance

  • 0

I hear (and read on this site) a lot about "favour composition over inheritance".

But what is composition? I understand inheritance from the point of Person : Mammal : Animal, but I can’t really see the definition of composition anywhere…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:37:15+00:00Added an answer on May 16, 2026 at 4:37 am

    Composition refers to combining simple types to make more complex ones. In your example, composition could be:

    Animal:
        Skin animalSkin
        Organs animalOrgans
    
    
    Mammal::Animal:
        Hair/fur mammalFur
        warm-blooded-based_cirulation_system heartAndStuff
    
    Person::Mammal:
        string firstName
        string lastName
    

    If you wanted to go totally composition (and get rid of all inheritance) it would look like this:

    Animal:
        Skin animalSkin
        Organs animalOrgans
    
    Mammal:
        private Animal _animalRef
        Hair/fur mammalFur
        warm-blooded-based_cirulation_system heartAndStuff
    
    Person:
        private Mammal _mammalRef
        string firstName
        string lastName
    

    The advantage to this approach is that the types Mammal and Person do not have to conform to the interface of their previous parent. This could be a good thing because sometimes a change to the superclass can have serious effects on the subclasses.

    They still can have access to the properties and behaviours of these classes through their private instances of these classes, and if they want to expose these former-superclass behaviours, they can simply wrap them in a public method.

    I found a good link with good examples in Composition versus Inheritance. A Comparative Look at Two Fundamental Ways to Relate Classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read many article about this one. I want to hear from you.
Point of HW is it should read this from a file: int func(int arg)
In informatics theory I hear and read about high-level and low-level languages all time.
I hear a lot about the wrap panel being slower to load things and
I hear much talk about memcached , but when briefly studied on it, I
I do not really know if this is scientifically proven, but I've read in
I've read a great deal of discussion recently (both on this site and elsewhere)
I have just read this [very nice] small tutorial about MYSQL error handling. I
I hear/read about it sometimes when talking about .NET, for example managed code and
every once in a while i read/hear about AWS and now i tried reading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.