0

For instance, would the following question be on topic?

I was recently exposed to the design principle "favour composition over inheritance", as well as some guidelines suggesting that highly nested inheritance trees should be avoided. However, I found this hard to follow when I tried to implement my own Matrix framework. Methods like get_element(row, col) can't be deleguated to a component unless the component knows and has access to the internal storage of the matrix. Since all the methods that should be advertised by the matrix are storage dependent, If the component was made to hold and manage the storage, the matrix will contain only this component, which seems like bad design.

Since Java's Collection framework solves a problem that is somewhat similar to the one I'm trying to solve, which relates to storing and retrieving a group of objects, I decided to take a look at how it was implemented and found that it contains a highly nested inheritance tree.

Could this framework have been developped by using composition over inhertiance? Is this framework one of the rare examples where the principle "favour composition over inheritance" is mistaken?

enter image description here

3
  • 1
    You may be interested in this old Stackoverflow Q&A
    – Doc Brown
    Aug 25, 2023 at 8:17
  • aren't there only three classes here which use inheritance?
    – Ewan
    Sep 10, 2023 at 19:05
  • HashSet wraps a HashMap! so its arguably using composition!
    – Ewan
    Sep 10, 2023 at 19:12

2 Answers 2

2

IMHO this question would be on-topic, but in the current form, at the the first glance, I think it looks like you want us to assess the design of your Matrix framework, which I find confusing. You may focus on presenting the core question first, and afterwards the motivation behind it. (Or you could ask a different question which presents the current Matrix design and ask if the application of "composition-over-inheritance" would be applicable.)

I would also recommend to proofread the question and use a spell checker, some community members seem to downvote questions simply on the basis of a few typos. Additionally, I would remove the assumption from the question that the number of cases where one should not favor composition over inheritance is "rare".

It is also not ideal that your question is literally a "yes-or-no" question. Question have a higher chance to get accepted when focussing on "how" or "why" instead of "can it be done?".

Finally, I cannot give you a guarantee, even when you follow my recommendations above, some community would not still vote to close this question as "opinionated", or for needing more focus. You surely don't want to know if the Java collection framework could have been implemented with "composition instead of inheritance" (for which the answer is obviously "yes"), but how it could have been done with less inheritance in a more useful fashion.

Though I personally think this would be sensible question, for others, this might be too opinionated. But it is definitely a very broad question, since discussing alternative class design can (and probably should) be done one class after another. For example, a more specific question could have been if it would have been beneficial not to derive Stack from Vector. Surprise - this is a question which was already asked in the past on Stackoverflow 13 years ago.

So before you now go to rewrite your question and ask it, I recommend to invest a little bit more time into research for existing questions about the design of the Java Collection Framework.

2
  • 1
    thing about Stack and Vector is, these are not an organic part of JCF, but merely a legacy forced by compatibility with earlier Java version
    – gnat
    Sep 1, 2023 at 7:38
  • 1
    although that older SO question does not focus on framework design, one of comments there hints at their legacy nature by pointing to recommendation for Deque, which is a truely organic replacement for Stack
    – gnat
    Sep 1, 2023 at 7:43
1

I think the problem you'll get with this question is really you want a discussion on the topic of whether "favour composition over inheritance" is a good or bad rule, when it can be applied etc.

Maybe there is a key difference and someone will say "ahhh! when you have X then composition is worse because Y"

But I think it unlikely.

Can you point to where your example of the collections framework has some functionality that couldn't be achieved well or at all with composition? The you could ask "can X be achieved with composition with out downside Y?" and get specific answers while basically having the same discussion about whether some proposed solution is good or bad

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .