When I was learning .NET, one of the things I learned early was that .NET does not support multiple inheritance. Multiple inheritance is having one class be inherited by more than one class. I never had a need to do anything like this for the longest time, so I never really thought twice about this.
Recently, however, I came across a potential use for multiple inheritance. I came across an open source library used in a project I was working on. The library was heavily modified, with multiple fields and methods, both private and public, being added, removed, and changed. It was pretty serious. Of course, the open source project was updated over the years, but since our copy of it was so heavily modified, we could not upgrade.
At first, I thought a good resolution would be splitting out the customized logic from the main project into a derived class, which would allow us to upgrade the project without breaking the custom logic. And this sounded great.
That is, until I realized that there were a number of classes in the open source project that ALREADY inherited from classes within the same project.
I now have a problem. Let's say I have 3 classes: * Class A, which is the base class from the open source project. * Class B, which is the class from the open source project that inherits the base class. * Class C, which is the base class with our customizations that inherits the open source project's base class.
I now need to implement the custmoizations that were made to Class B into a new class, which I'll call Class D. The customizations that were made to Class B depend on the customizations that exist in Class C, but I also need some of the base methods and fields in Class B. Ideally, I want to create Class D to inherit from BOTH Class B AND Class C. But I can't: .NET doesn't allow it. Why?
Because of something known as The Diamond Problem. If I create Class D which inherits from Class B and Class C, and each of those classes inherit from Class A, I could theoretically create methods in Class B and Class C that are based on methods in Class A. When I call those methods from Class D, how do I know whether I need to call the method from Class B or from Class C?
It's called The Diamond Problem because of how the class diagram would look if you mapped out the classes: Class A would be the top point, Class B and Class C would be the left and right points with a line coming to each of them from Class A, and Class D at the bottom with a line coming to it from each of Class B and Class C.
The problem is clear. However, the solution is not. There is no clear cut way for me to construct a Class D that has the functionality I need. There are some very ungraceful workarounds, including copy and pasting code, using multiple interface inheritance, or instantiating the classes you want to inherit as properties of the derived class. None of these solutions are perfect for varying reasons.
In this case, we want to be able to update the open source project as needed, but we want to maintain the customizations that were made to it. Other than multiple inheritance, how would you resolve this?
This is my blog where I give my thoughts and opinions on various topics and share my creative endeavors with the world. I run two personal blogs, but combine them here for ease of access.
Blogger - My oldest blog using the Blogger platform contains posts full of opinions, gaming, and code.
Tumblr - Tumblr posts are all about my creative side, containing music, videos, writings, and updates on my web creations.
You can select a category below to view the latest post, or browse thorugh the posts using the navigation found at the top and bottom of each post.
In California, truck license plates are 7 characters, all numbers except for one letter in either the second or the sixth position. Whenever that letter is an X, I get nerd snipped into treating it as a multiplication problem and solving it in my head.
we should have paid more attention to the cats who, for decades, put their bodies on the line to walk on keyboards and sit on laptops and prevent us from programming
Former 2 time world champion DogPlayingTetris becomes the first player to ever rollover the level counter in NES Tetris, performing what's known in the community as "Rebirth". Final score: 29,486,164, 4216 lines, level 347 (256 + 91)... all huge world records. #tetris
I'd also love a 6 hour layover overnight instead of taking the red eye I was going to take and be 7 hours later getting into Cleveland than I wanted, why do you ask? #airporthell
Why yes, I'd love to leave at 4:40 to get to the airport at 6:20 for an 8:20 flight that got delayed to 9:05 which is too late for my connection so now I'm on a 10:20 flight instead. Why do you ask? #airporthell
@shanselman Who at Microsoft do I have to bribe to fix ADO so that those of us on dark mode who copy/paste text from one task to another can do so without our friends on light mode seeing dark text on a dark background?
I updated the blog post with a statement from Revival. While I'm not particularly happy with Revival's decision, I understand their motives. It's just a shame that it was someone from Interplay that had to go and do this. "By games for gamers" my ass.
Damn, got another Tetris world record! This time in the arcade variant developed by Atari. 6,008,005 points, 5,386 lines, round 363. Be warned, it's nearly FIVE HOURS. https://www.twitch.tv/videos/2131759212