{"id":23780,"date":"2026-06-03T04:23:12","date_gmt":"2026-06-03T04:23:12","guid":{"rendered":"https:\/\/sharpinterview.online\/?sharp_quiz=java-interview-inheritance-composition-pitfall"},"modified":"2026-06-03T04:23:12","modified_gmt":"2026-06-03T04:23:12","slug":"java-interview-inheritance-composition-pitfall","status":"publish","type":"sharp_quiz","link":"https:\/\/sharpinterview.online\/?sharp_quiz=java-interview-inheritance-composition-pitfall","title":{"rendered":"Java: Which practice avoids a common mistake with Inheritance vs Composition?"},"content":{"rendered":"<h2>Question<\/h2>\n<p>Which practice avoids a common mistake with Inheritance vs Composition?<\/p>\n<ol type=\"A\">\n<li>Ignore the Inheritance vs Composition issue and rely on team discipline instead of APIs or contracts.<\/li>\n<li>Silence the Inheritance vs Composition problem by using raw types, broad catches, or shared mutable state.<\/li>\n<li>Prefer the version of Inheritance vs Composition that makes behavior less predictable as long as the code compiles.<\/li>\n<li>Do not inherit only to reuse code if the subtype cannot preserve the parent contract naturally.<\/li>\n<\/ol>\n<h2>Hint<\/h2>\n<p>Look for the option that protects correctness instead of hiding the problem.<\/p>\n<h2>Answer and rationale<\/h2>\n<p><strong>Correct answer:<\/strong> D. Do not inherit only to reuse code if the subtype cannot preserve the parent contract naturally.<\/p>\n<p>Do not inherit only to reuse code if the subtype cannot preserve the parent contract naturally. This is a common failure mode in real Java code and a frequent interview follow-up.<\/p>\n<p><strong>Track:<\/strong> Java<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Which practice avoids a common mistake with Inheritance vs Composition?<\/p>\n","protected":false},"author":1,"featured_media":0,"template":"","topic":[3,4,16],"difficulty":[7],"class_list":["post-23780","sharp_quiz","type-sharp_quiz","status-publish","hentry","topic-java","topic-java-interview-questions","topic-oop-inheritance-vs-composition","difficulty-medium"],"_links":{"self":[{"href":"https:\/\/sharpinterview.online\/index.php?rest_route=\/wp\/v2\/sharp_quiz\/23780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sharpinterview.online\/index.php?rest_route=\/wp\/v2\/sharp_quiz"}],"about":[{"href":"https:\/\/sharpinterview.online\/index.php?rest_route=\/wp\/v2\/types\/sharp_quiz"}],"author":[{"embeddable":true,"href":"https:\/\/sharpinterview.online\/index.php?rest_route=\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/sharpinterview.online\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23780"}],"wp:term":[{"taxonomy":"topic","embeddable":true,"href":"https:\/\/sharpinterview.online\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=23780"},{"taxonomy":"difficulty","embeddable":true,"href":"https:\/\/sharpinterview.online\/index.php?rest_route=%2Fwp%2Fv2%2Fdifficulty&post=23780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}