Deep C# - Interface |
Written by Mike James | |||||||
Tuesday, 12 April 2022 | |||||||
Page 3 of 3
The Diamond Problem – Explicit ImplementationThe big problem with multiple inheritance of any kind is the diamond problem. It’s name comes from the shape of the inheritance diagram you get if you draw it out. At its most general, it is what happens if A is inherited by B and C and then D inherits from both B and C. In this case D gets two copies of A. Equally problematic, however, is what is to happen if B and C have the same method complete with the same signature? This is a problem that can easily happen with interfaces but the solution in this case is trivial – as the inheriting class has to implement the method this new implementation becomes the defining one. However, what if this isn’t what you want? Suppose that a class inherits from two interfaces, IMyInterfaceA and IMyInterfaceB, both with myMethod with the same signature and suppose also that myMethod does different things in each interface – one is say equality of value and the other equality of reference. Then providing a single implementation which does for both isn’t going to work. The solution C# adopts it to allow you to define both versions of the method, but as members of the interface types rather than of the class. For example: class MyClassA : IMyInterfaceA, IMyInterfaceB { int IMyInterfaceA.myMethod() { throw new NotImplementedException(); } int IMyInterfaceB.myMethod() { throw new NotImplementedException(); } } This is called an explicit interface implementation. Now there is no myMethod defined within class MyClassA, only two different interface methods: MyClassA myObject = new MyClassA(); Console.WriteLine( Having to cast to an interface type before you can use the correct method isn’t elegant or convenient, but it does allow you to pass the class to a function that is expecting an object with that particular method implemented. You don’t have to use an explicit implementation for all of the identical methods inherited from different interfaces, you can provide a class implementation which is used if the class isn’t cast to the interface type. In book but not in this extract
Postlude Inheritance for code reuse really does need to be multiple inheritance, but it is a difficult tool to work with due to the diamond problem among others. C#’s single inheritance is tempered by the use of interfaces which, despite having default methods, don’t help much with code reuse. What they do is to allow type safety with multiple inheritance but without the need to worry about selecting between multiple inherited implementations. The use of interfaces as types makes the type hierarchy less of a strict hierarchy.
Deep C#Buy Now From Amazon Chapter List
Extra Material <ASIN:1871962714> <ASIN:B09FTLPTP9> To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |
|||||||
Last Updated ( Tuesday, 12 April 2022 ) |