[JAVA] Which is better, generalization or delegation, when there are overlaps?

When duplicate functions appear during object-oriented implementation You may be wondering how to put it together. As a method, "generalization" that creates a class with the common part as a parent There is a "delegation" that cuts out the common part into an outer class and refers to it.

Generalization

図4.png

transfer

図5.png

In general, ** delegation ** is better. The reason is that in the relation of ** inheritance **, it can be reused only by parent and child, but if it is ** delegation **, it can be reused from other classes. In terms of inheritance and generalization, it is said that changing the superclass will affect children and increase the range of influence. Also, the degree of coupling between superclasses and subclasses is stronger than delegation, and delegation also maintains independence.

Recommended Posts

Which is better, generalization or delegation, when there are overlaps?
Which is better, Kotlin or Java in the future?
The question of which is better, if or switch
[6 selections of form tool comparison] Which is better, open source or commercial?