<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6" -->

---
title: Why should you favor composition over inheritance? -...
description: An interview-style explanation argues developers should favor composition over inheritance because it makes code easier to discard when requirements change....
canonical: https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Why should you favor composition over inheritance? - Cracking the Java Coding Interview #javacoding | daily.dev
og:description: An interview-style explanation argues developers should favor composition over inheritance because it makes code easier to discard when requirements change....
og:url: https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6
og:image: https://api.daily.dev/og/posts/wlkSlkqD6.png
og:image:alt: Why should you favor composition over inheritance? - Cracking the Java Coding Interview #javacoding
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Why should you favor composition over inheritance? - Cracking the Java Coding Interview #javacoding

**[Java \(Official Oracle\)](https://daily.dev/sources/java-oracle)** · 2 min read · 8 upvotes · 2 comments

## Summary

An interview-style explanation argues developers should favor composition over inheritance because it makes code easier to discard when requirements change. Using an example of switching from XML to JSON marshaling over a 20-year span, it shows how an inheritance-based to-XML method becomes stranded dead code, while a delegate-based factory class can simply be thrown away. The piece ties this advice back to the Gang of Four design patterns book published in 1994, framing composition over inheritance as a long-established best practice worth following even without fully understanding the reasoning.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=DIZTQxkOsy4>

## Questions this post answers

### Why is composition over inheritance recommended in object-oriented design?

Composition over inheritance is recommended because it makes decoupling application modules easier and makes unused code simpler to remove. For example, if a class has an inheritance-based to-XML method that becomes obsolete after switching to JSON, removing it is costly because other code depends on it directly. If that behavior instead lived in a separate delegate or factory class, the whole class could simply be discarded once nobody calls it, avoiding lingering dead code.

_Developers weighing design tradeoffs like this can find deeper OOP discussions curated on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@calindinis** · 1 upvotes

> Yeah, if any one of us would still know how to code, this would have been a good read.

**@petermrozek** · 0 upvotes

> It's worth mentioning that favouring composition over inheritance eases the transition to languages like Rust where OOP is not present. That being said, there's always a golden centre - there are cases where inheritance is preferred (for instance, an unchanging hierarchy where one class is always a specialisation of another - a "is a" relationship, if you will).

## Similar posts on daily.dev

- [When did people favor composition over inheritance?](https://daily.dev/posts/when-did-people-favor-composition-over-inheritance--mcnlqine3) · Hacker News · 41 upvotes · 6 comments
- [Why Your Software Sucks: Inheritance](https://daily.dev/posts/why-your-software-sucks-inheritance-pnnmgu4k8) · Aaronontheweb · 28 upvotes · 3 comments
- [A JavaScript developer's guide to Rails: What does Composition Over Inheritance mean?](https://daily.dev/posts/a-javascript-developer-s-guide-to-rails-what-does-composition-over-inheritance-mean--8ira41rnr) · thoughbot · 2 upvotes · 0 comments
- [Design Patterns Suck](https://daily.dev/posts/design-patterns-suck-kei3cx9df) · iamluminousmen · 1 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java), [#interview-questions](https://daily.dev/tags/interview-questions)

[View this post on daily.dev](https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Why should you favor composition over inheritance? - Cracking the Java Coding Interview #javacoding","url":"https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6"},"datePublished":"2026-09-18T10:02:42.746Z","dateModified":"2026-09-18T10:02:59.264Z","description":"An interview-style explanation argues developers should favor composition over inheritance because it makes code easier to discard when requirements change....","image":"https://i.ytimg.com/vi/DIZTQxkOsy4/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/DIZTQxkOsy4/sddefault.jpg","isAccessibleForFree":true,"articleSection":"Java (Official Oracle)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Java (Official Oracle)","logo":"https://media.daily.dev/image/upload/s--SM9hIYUY--/f_auto,q_auto/v1773648791/logos/java-oracle?_a=BAMAMiiu0","url":"https://daily.dev/sources/java-oracle"},"commentCount":2,"discussionUrl":"https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":8},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":2}],"keywords":"java,interview-questions","timeRequired":"PT2M","video":{"@type":"VideoObject","name":"Why should you favor composition over inheritance? - Cracking the Java Coding Interview #javacoding","description":"An interview-style explanation argues developers should favor composition over inheritance because it makes code easier to discard when requirements change....","thumbnailUrl":"https://i.ytimg.com/vi/DIZTQxkOsy4/sddefault.jpg","uploadDate":"2026-09-18T10:02:42.746Z","duration":"PT2M","url":"https://api.daily.dev/r/wlkSlkqD6","embedUrl":"https://www.youtube.com/embed/DIZTQxkOsy4"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Java (Official Oracle)","item":"https://daily.dev/sources/java-oracle"},{"@type":"ListItem","position":3,"name":"Why should you favor composition over inheritance? - Cracking the Java Coding Interview #javacoding"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6","comment":[{"@type":"Comment","text":"Yeah, if any one of us would still know how to code, this would have been a good read.","datePublished":"2026-09-18T12:26:19.060Z","url":"https://daily.dev/posts/wlkSlkqD6#c-nnnZC3sWN","author":{"@type":"Person","name":"Calin Dinis","url":"https://daily.dev/calindinis","image":"https://lh3.googleusercontent.com/a/ACg8ocIivX4QCjrIZwuMY7skYlJAYJ1qH_yA2IQ3XcT9DQb4vQPM4w=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"It’s worth mentioning that favouring composition over inheritance eases the transition to languages like Rust where OOP is not present. That being said, there’s always a golden centre - there are cases where inheritance is preferred (for instance, an unchanging hierarchy where one class is always a specialisation of another - a “is a” relationship, if you will).","datePublished":"2026-09-18T15:15:20.946Z","url":"https://daily.dev/posts/wlkSlkqD6#c-a5iGmiXC6","author":{"@type":"Person","name":"Peter Mrożek","url":"https://daily.dev/petermrozek","image":"https://media.daily.dev/image/upload/s--pBfYX68K--/f_auto/v1769247960/avatars/avatar_Qz65P1nVw3Bu6C5YwaZgA?_a=BAMAMiiu0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/why-should-you-favor-composition-over-inheritance---cracking-the-java-coding-interview-javacoding-wlkslkqd6#faq","mainEntity":[{"@type":"Question","name":"Why is composition over inheritance recommended in object-oriented design?","acceptedAnswer":{"@type":"Answer","text":"Composition over inheritance is recommended because it makes decoupling application modules easier and makes unused code simpler to remove. For example, if a class has an inheritance-based to-XML method that becomes obsolete after switching to JSON, removing it is costly because other code depends on it directly. If that behavior instead lived in a separate delegate or factory class, the whole class could simply be discarded once nobody calls it, avoiding lingering dead code. Developers weighing design tradeoffs like this can find deeper OOP discussions curated on daily.dev."}}]}
```

