How to solve this diamond inheritance problem

Asked

Viewed 236 times

0

Can anyone solve this problem (diamond heritage) with the use of Interfaces and post here in c#? I found it in this post: Why C# does not allow multiple inheritances? but I couldn’t solve it.

Inheriting many concrete classes is a major source of problems from the point of view of the design of a programming language: what to do if the two superclasses have a method of the same name? What if you have instance variables of the same name? A particularly complicated problem is diamond heritage. Consider the following inheritance pattern:

  A
 / \
B   C
 \ /
  D

A is a superclass that defines a virtual method foo(). B and C are classes that inherit from A and reimplementam foo. Finally, D is a class that inherits multiple of B and C. Now, if we do

A obj = new D();

obj.foo();

Which version of the method is called? The version defined in B or the one defined in C?

Due to these complications, many programming languages (including C# and Java) prefer to make things simpler and allow simple inheritance only.

That said, it may be that the language provides alternatives to some of the more common uses of multiple inheritance. For example, C# allows a class to implement more than one interface, which is similar to inheriting multiples of purely abstract classes.

  • Could you please post the post that has the solution in c# of this problem that I put here? The two who negatively posted. I don’t care about points, I just want guidance.

  • 2

    The problem cannot be reproduced because it is not possible to inherit two classes... you could inherit two interfaces, but on the interface there is no implementation... ps. No negativei

  • But there is no solution (achieve the same goal) with the use of interfaces?

  • Well, I will research more, I am very flustered. When I come across a question I already want to ask someone who knows the subject.

  • 1

    no... on the interface there is no implementation, there is no overriding... see these two examples: https://dotnetfiddle.net/nqYjvO and https://dotnetfiddle.net/qlz4Wk

  • 1

    c# does not inherit two classes, so this cannot be solved in this language. "C# allows a class to implement more than one interface, which is similar to inheriting multiples of purely abstract classes", yes it is true, the basic difference was that an abstract class can have implemented operations, behavior, while the interface does not, until recently, when the interfaces in . net now allow implementations, I even asked a question about it: standard implentations with c#

  • 1

    @Rovannlinhalis "interface has no implementation", now have with c#8 standard implentations with c#

  • 1

    @Ricardopunctual, yes I had seen his question including I gave +1 there... but I have no information of what would be the behavior in this situation, so I preferred to keep the reference of the previous versions. Once I have access to C# 8, I intend to take that test

  • 1

    I just thought this new feature kind of kills the abstract class. It has to see in depth what can be implemented, but if there are no limitations, the difference in the abstract class ceases to exist

  • I have no idea what the doubt is here. A lot has been said, with good reason, there is a question that is rhetorical, there are others that are not possible in C#. Question I didn’t see.

  • Reading the comments here I say that is not quite so :)

  • My question is whether there is how to make this diamond heritage with the use of interfaces.

Show 7 more comments

1 answer

3

Today, this is not possible in the language, however, in version 8 of C#, it will be possible to have the default implementation of methods in interfaces, anyway, one of the interfaces should be implemented explicitly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.