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

  • Home
  • SEARCH
  • 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 66911

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:10:39+00:00 2026-05-10T19:10:39+00:00

There are two schools of thought on how to best extend, enhance, and reuse

  • 0

There are two schools of thought on how to best extend, enhance, and reuse code in an object-oriented system:

  1. Inheritance: extend the functionality of a class by creating a subclass. Override superclass members in the subclasses to provide new functionality. Make methods abstract/virtual to force subclasses to ‘fill-in-the-blanks’ when the superclass wants a particular interface but is agnostic about its implementation.

  2. Aggregation: create new functionality by taking other classes and combining them into a new class. Attach an common interface to this new class for interoperability with other code.

What are the benefits, costs, and consequences of each? Are there other alternatives?

I see this debate come up on a regular basis, but I don’t think it’s been asked on Stack Overflow yet (though there is some related discussion). There’s also a surprising lack of good Google results for it.

  • 0 0 Answers
  • 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. 2026-05-10T19:10:39+00:00Added an answer on May 10, 2026 at 7:10 pm

    It’s not a matter of which is the best, but of when to use what.

    In the ‘normal’ cases a simple question is enough to find out if we need inheritance or aggregation.

    • If The new class is more or less as the original class. Use inheritance. The new class is now a subclass of the original class.
    • If the new class must have the original class. Use aggregation. The new class has now the original class as a member.

    However, there is a big gray area. So we need several other tricks.

    • If we have used inheritance (or we plan to use it) but we only use part of the interface, or we are forced to override a lot of functionality to keep the correlation logical. Then we have a big nasty smell that indicates that we had to use aggregation.
    • If we have used aggregation (or we plan to use it) but we find out we need to copy almost all of the functionality. Then we have a smell that points in the direction of inheritance.

    To cut it short. We should use aggregation if part of the interface is not used or has to be changed to avoid an illogical situation. We only need to use inheritance, if we need almost all of the functionality without major changes. And when in doubt, use Aggregation.

    An other possibility for, the case that we have an class that needs part of the functionality of the original class, is to split the original class in a root class and a sub class. And let the new class inherit from the root class. But you should take care with this, not to create an illogical separation.

    Lets add an example. We have a class ‘Dog’ with methods: ‘Eat’, ‘Walk’, ‘Bark’, ‘Play’.

    class Dog   Eat;   Walk;   Bark;   Play; end; 

    We now need a class ‘Cat’, that needs ‘Eat’, ‘Walk’, ‘Purr’, and ‘Play’. So first try to extend it from a Dog.

    class Cat is Dog   Purr;  end; 

    Looks, alright, but wait. This cat can Bark (Cat lovers will kill me for that). And a barking cat violates the principles of the universe. So we need to override the Bark method so that it does nothing.

    class Cat is Dog   Purr;    Bark = null; end; 

    Ok, this works, but it smells bad. So lets try an aggregation:

    class Cat   has Dog;   Eat = Dog.Eat;   Walk = Dog.Walk;   Play = Dog.Play;   Purr; end; 

    Ok, this is nice. This cat does not bark anymore, not even silent. But still it has an internal dog that wants out. So lets try solution number three:

    class Pet   Eat;   Walk;   Play; end;  class Dog is Pet   Bark; end;  class Cat is Pet   Purr; end; 

    This is much cleaner. No internal dogs. And cats and dogs are at the same level. We can even introduce other pets to extend the model. Unless it is a fish, or something that does not walk. In that case we again need to refactor. But that is something for an other time.

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

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer For those who are interested, I found one semi-working ATL… May 11, 2026 at 10:14 am
  • added an answer There is no way to resolve this - only workarounds.… May 11, 2026 at 10:14 am
  • added an answer By putting the text inside a DIV with a style… May 11, 2026 at 10:14 am

Related Questions

There are two schools of thought on how to best extend, enhance, and reuse
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There are two popular naming conventions: vc90/win64/debug/foo.dll foo-vc90-win64-debug.dll Please discuss the problems/benefits associated with
There are two scenarios I need to clarify: An executable compiled with .NET 3.5
There are two databases in SQL Server 2005: One called A and another one
There are two different ways to create an empty object in JavaScript: var objectA
There are two pictureboxes with two different images. If I click on one picture
Suppose there are two scripts Requester.php and Provider.php, and Requester requires processing from Provider
Backdrop: There are two forms, the main application form and a form to edit

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.