By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright ยฉ All Rights Reserved. World of Software.
Reading: Code Smell 290 – Refused Bequest | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright ยฉ All Rights Reserved. World of Software.
World of Software > Computing > Code Smell 290 – Refused Bequest | HackerNoon
Computing

Code Smell 290 – Refused Bequest | HackerNoon

News Room
Last updated: 2025/02/12 at 7:45 PM
News Room Published 12 February 2025
Share
SHARE

When you ignore your inheritance, you will have trouble with your parents

TL;DR: Subclasses should honor ALL their parentโ€™s contract.

Problems ๐Ÿ˜”

Solutions ๐Ÿ˜ƒ

  1. Favor composition over inheritance
  2. Don’t subclassify for code reuse
  3. Rethink hierarchy
  4. Extract shared logic
  5. Use interfaces
  6. Remove dead code

Refactorings โš™๏ธ

Refactoring 007 – Extract Class

Context ๐Ÿ’ฌ

When you create a subclass, it should use or extend the behavior of its parent.

If it ignores or overrides most of it, you probably force inheritance where it doesnโ€™t belong to reuse code.

This makes the code misleading and hard to maintain.

Sample Code ๐Ÿ“–

Wrong ๐Ÿšซ

class House {
  constructor(address) {
    this.address = address;
  }
  address() {
    return this.address;
  }
  openDoor() {
    console.log("Door opened at " + this.address);
  }
}

class Motorhome extends House {
  constructor() {
    super(null);
  }
  address() {
    return null;
    // This implementation is the same as the parent's
    // and is also a refused bequest
  }
  openDoor() {
    console.log("Motorhome door opened.");
  }
}

Right ๐Ÿ‘‰

class House {
  constructor(address) {
    this.address = address;
  }
  address() {
    return this.address;
  }
  openDoor() {
    console.log("Door opened at " + this.address);
  }
}

class Motorhome {
  // does not inherit from House
  constructor(gps) {
    this.gps = gps;
  }
  openDoor() {
    console.log("Motorhome door opened at " + this.gps.getLocation());
  }
}

Detection ๐Ÿ”

Look for subclasses that override or ignore most of their parentโ€™s behavior.

You should reconsider the inheritance if a subclass sets parent properties to null or reimplements core methods.

Level ๐Ÿ”‹

Why the Bijection Is Important ๐Ÿ—บ๏ธ

Your software should reflect real-world relationships.

When you force a subclass that doesnโ€™t logically extend its parent in the Bijection, you mislead developers and introduce maintenance problems.

AI Generation ๐Ÿค–

AI can generate this smell when it defaults to inheritance for reuse instead of composition.

This happens when AI follows generic templates without understanding the context.

AI Detection ๐Ÿฅƒ

AI can detect this smell by analyzing class structures and inheritance trees. However, it struggles with subtle cases where inheritance initially seems valid but breaks expectations.

Try Them! ๐Ÿ› 

Remember: AI Assistants make lots of mistakes

Conclusion โœ”๏ธ

When you design a class hierarchy, you need to make sure that subclasses logically inherit from their parent.

If a subclass refuses some of the behavior, you should rethink your design.

Relations ๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ’‹โ€๐Ÿ‘จ

Code Smell 11 – Subclassification for Code Reuse

Code Smell 58 – Yo-yo Problem

Code Smell 43 – Concrete Classes Subclassified

More Information ๐Ÿ“•

Refactoring Guru

Code Smells

Disclaimer ๐Ÿ“˜

Code Smells are my opinion.

Credits ๐Ÿ™

Photo by Hanson Lu on Unsplash


Favor object composition over class inheritance.

Erich Gamma

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code


Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Saros is the next big PS5 game from Returnal’s Housemarque, coming 2026
Next Article Elon Muskโ€™s X Settles Trump Lawsuit
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Digital Defenders: Meet Syed Shahzaib Shah, Pakistanโ€™s Ethical Hacker Changing the Game | HackerNoon
Computing
Apple Acquisition Hints at Upgraded Calendar App on iOS 19 or Beyond
News
Ring is giving away FREE outdoor cameras worth ยฃ80 – and it’s easy to apply
News
How to Use Your iPad as a Second Monitor With Your Mac
Gadget

You Might also Like

Computing

Digital Defenders: Meet Syed Shahzaib Shah, Pakistanโ€™s Ethical Hacker Changing the Game | HackerNoon

6 Min Read
Computing

Everything I heard at the AVCA Conference |

9 Min Read
Computing

The Best Way to Protect Your Packages and Your Ethics | HackerNoon

8 Min Read
Computing

Why Glovo thinks Nigeria is its biggest African bet yet

11 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright ยฉ All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?