IntelliJ IDEA Tips & Tricks #58: Automatically Replace Inheritance with Delegation!

Опубликовано: 20 Октябрь 2024
на канале: CodeOpsTech
1,015
6

Often, we use inheritance inappropriately for code reuse. When the base and derived classes don't share a IS-A relationship, we need to refactor the code to use delegation instead to have HAS-A relationship.



Consider that a Poster class extends Image class. Rather, a Poster HAS-A Image object and so delegation can be used instead. However, instead of doing this refactoring manually, you can use IntelliJ IDEA to do it for you. Just invoke the "Refactor This" context menu action, and convert that inheritance use to delegation instead.