Deep C# - Interface
Written by Mike James   
Tuesday, 12 April 2022
Article Index
Deep C# - Interface
Multiple Inheritance
The Diamond Problem

The Diamond Problem – Explicit Implementation

The 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.

tree4

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(
(myObject as IMyInterfaceA).myMethod()); Console.WriteLine(
(myObject as IMyInterfaceB).myMethod());

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

  • Default Methods
  • What Can An Interface Do?

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

DeepCsharp360

 Chapter List

  1. Why C#?

    I Strong Typing & Type Safety
  2. Strong Typing
       Extract 
    Why Strong Typing
  3. Value & Reference
  4.    Extract Value And Reference
  5. Structs & Classes
       Extract
    Structs & Classes 
  6. Inheritance
      
    Extract
    Inheritance
  7. Interfaces & Multiple Inheritance
      
    Extract Interface
  8. Controlling Inheritance
    II Casting & Generics
  9. Casting - The Escape From Strong Typing
      
    Extract Casting I ***NEW!
  10. Generics
  11. Advanced Generics
  12. Anonymous & Dynamic
    Typing
    III Functions
  13. Delegates
  14. Multicast Delegates
  15. Anonymous Methods, Lambdas & Closures
    IV Async
  16. Threading, Tasks & Locking
  17. The Invoke Pattern
  18. Async Await
  19. The Parallel For
    V Data - LINQ, XML & Regular Expressions
  20. The LINQ Principle
  21. XML
  22. LINQ To XML
  23. Regular Expressions
    VI Unsafe & Interop
  24. Interop
  25. COM
  26. Custom Attributes
  27. Bit Manipulation
  28. Advanced Structs
  29. Pointers 

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.

Banner


Quantum Computing Prize Awarded
05/04/2024

John Preskill, Professor of Theoretical Physics at the California Institute of Technology, is the eighth recipient of the John Stewart Bell Prize for Research on Fundamental Issues in Quantu [ ... ]



Falco On Track To Version 1.0.0
02/04/2024

Falco is a cloud native runtime security tool for the Linux operating system, designed to detect abnormal behavior and warn of potential security threats in real-time. Now it's about to release its fi [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Tuesday, 12 April 2022 )