Skip to content

is there a preferable relationship between classes? Why? [closed]

is there really a preferable relationship between classes ? or it depends on the software we have ?
I know that we have is-a and has-a relationships in classes relations but, is there a one relation that is Confident and it most preferable between software-designers.


The only preferred relationship between classes is independence. Because independence means guranteed separation of concerns and freedom to evolve. (Joke)

But unfortunately independence is not very useful: Lots of lonesome classes will only help to solve lots of little isolated problems. If you really want to make something useful, you’ll have to relate the right classes. And then, the only thing that matters is what relationship helps you to best address your needs. Sometimes it’s inheritance (is-a), sometimes it’s composition (has-a). It all depends on the context.

What your “doctor” probably meant was to prefer composition over inheritance. This is a useful advice. But it is a simple rule of thumb: it is not a universal truth.