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.
Asked
Active
Viewed 78 times
-1
-
3Like most things in software development, it depends on what your specific requirements are. Your goal should be to find the way that makes the most sense for your specific application, not to discover The One Ring to Rule Them All.™ – Robert Harvey Oct 05 '20 at 17:16
-
my doctor says Dependency is the preferable one between classes because it defines what most would agree is the loosest form of dependency between classes I'm confused about that, so is he wrong ? – Amro Okasha Oct 05 '20 at 17:41
-
2Yes, your "doctor" is wrong. He should be teaching you how to think and make decisions for yourself, not just blindly follow some principles. – Robert Harvey Oct 05 '20 at 17:43
-
2Does this answer your question? [Prefer composition over inheritance?](https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – jaco0646 Oct 05 '20 at 19:18
1 Answers
3
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.

Christophe
- 68,716
- 7
- 72
- 138