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 8088253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:01:57+00:00 2026-06-05T19:01:57+00:00

Possible Duplicate: Prefer composition over inheritance? I wonder, why (or in which cases) should

  • 0

Possible Duplicate:
Prefer composition over inheritance?

I wonder, why (or in which cases) should one consider inheritance instead of composition when there are so much cons of it:

  • if we implement/override a method in a subclass which calls the method of the superclass, there’s no guarantee that another version of our superclass (maybe some library) won’t break our code
  • if there will appear a new method in the superclass with the same signature that the sublclass method has, but with different return type, our class won’t compile.

Therefore, I can’t imagine, how on Earth we can rely on it. The superclass author may want to improve the performance and our client code may break down.

So my questions are:

  • How are these problems solved (e.g. in standard Java libraries)?
  • When to use inheritance and composition?
  • 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-06-05T19:01:59+00:00Added an answer on June 5, 2026 at 7:01 pm

    Your first objection also applies to composition: if the implementation of a method that you call changes, there’s no guarantee that your code won’t break.

    The second objection is actually a good thing, since you’ll immediately notice there is a problem with the new version of the API.

    The problem doesn’t have much to do with inheritance/composition. If the public contract of a class changes from version to version, you’ll have to make changes in your code to accomodate these changes. The problem is solved by avoiding changes that would be backward-incompatible. Providing a new API can be the solution (See io, followed by nio, followed by nio2, for example). Otherwise, release notes and migration documentation are there to help transitioning from one version of an API to another.

    Inheritance is used when there is a is-a relationship between your class and the other class. Composition is used when there is a has-a relationship between your class and the other class.

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

Sidebar

Related Questions

Possible Duplicate: When should one use final? When should Java programmers prefer to use
Possible Duplicate: When to prefer JSON over XML? i wonder if JSON is the
Possible Duplicate: C++ When should we prefer to use a two chained static_cast over
Possible Duplicate: decimal vs double! - Which one should I use and when? I'm
Possible Duplicate: Should I always prefer MySQL InnoDB over MyISAM? Under what conditions should
Possible Duplicate: C++ singleton vs completely static object Hi, why should I prefer a
Possible Duplicate: String vs string in C# Hello, Should I prefer string or String
Possible Duplicate: Is there any reason to use C instead of C++ for embedded
Possible Duplicate: New Cool Features of C# 4.0 Hello, There are several(many) questions at
Possible Duplicate: B- trees, B+ trees difference What are the advantages/disadvantages of BTree+ over

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.