.The first OO language I used was once C++ and then Smalltalk and in the end .NET and Java.
I was once gung-ho to leverage the advantages of Inheritance, Encapsulation, and Polymorphism. The Three Pillars of the Paradigm.
I used to be keen to attain the promise of Reuse and leverage the knowledge received by way of those who got here earlier than me in this new and interesting landscape.
I couldn’t include my pleasure at the idea of mapping my real-world objects into their Classes and anticipated the total world to fall neatly into place.
At first glance, Inheritance seems to be the largest gain of the Object Oriented Paradigm. All the simplistic examples of structure hierarchies that are paraded out as examples to the newly indoctrinated appear to make logical sense.
Banana Monkey Jungle Problem
With faith in my coronary heart and issues to solve, I began constructing Class Hierarchies and writing code. And all was once proper with the world.
I’ll in no way overlook that day when I used to be equipped to money in on the promise of Reuse with the aid of inheriting from an current class. This used to be the second I had been ready for.
A new assignment got here alongside and I concept lower back to that Class that I was once so fond of in my final project.
No problem. Reuse to the rescue. All I gotta do is without a doubt snatch that Class from the different challenge and use it.
Well… actually… no longer simply that Class. We’re gonna want the mother or father Class. But… But that’s it.
Ugh… Wait… Looks like we gonna additionally want the parent’s father or mother too... And then… We’re going to want ALL of the parents. Okay… Okay… I take care of this. No problem.
And great. Now it won’t compile. Why?? Oh, I see… This object includes this different object. So I’m gonna want that too. No problem.
Wait… I don’t simply want that object. I want the object’s dad or mum and its parent’s mother or father and so on and so on with each contained object and ALL the dad and mom of what these comprise alongside with their parent’s, parent’s, parent’s…
There’s a exquisite quote with the aid of Joe Armstrong, the creator of Erlang:
The hassle with object-oriented languages is they’ve obtained all this implicit surroundings that they raise round with them. You desired a banana however what you bought was once a gorilla conserving the banana and the complete jungle.
Banana Monkey Jungle Solution
I can tame this problem by means of no longer developing hierarchies that are too deep. But if Inheritance is the key to Reuse, then any limits I area on that mechanism will without a doubt restrict the advantages of Reuse. Right?
Right.
So what’s a negative Object Oriented Programmer, who’s had a healthful supporting of the Kool-aid, to do?
Contain and Delegate. More on this later.
The Diamond Problem
Sooner or later, the following hassle will rear its unsightly and, relying on the language, unsolvable head.

1 Comments
The thing is that Inheritance is not the key to reuse. Here one of the principles of the object oriented design: "Favor object composition over class inheritance" from GOF. In most of cases you would like to limit the use of Inheritance in favor of the "Low coupling" principle.
ReplyDelete