1

When a base class contains a virtual method, to add a more derived version in a sub class, we have to use override.

AFAICT, this serves two purposes:

  • We don't accidentally overide a base method
  • If we mistype the method name when we want to override, we get an error (similar to Java, right?)

However, to my dismay it seems that (in VS2010 / .NET4) it is not possible to use override when implementing an interface method.

Obviously the first bullet is rather a non issue, but the overridekeyword would have served as a good simple documentation and check that the interface methods are actually these that are marked as override.

So, when looking at a class implementation, is there any way other than a // comment to indicate that this method implements the method of a certain interface?

Community
  • 1
  • 1
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • There could be more that one implementation of particular interface in the class. Which methods should be marked with your "override"? – Dennis Oct 08 '14 at 13:13
  • @DEnnis - you mean when two interfaces ha a method with the same signature and the class implements both? – Martin Ba Oct 08 '14 at 13:14
  • Well, for starters, you don't *override* interface methods. The interfaces don't have actual methods, just the definitions of them. You *implement* those methods. A base class' `virtual` method has an actual implementation that you may or may not wish to `override`. Since interface methods are "overridden" I imagine requiring the `override` keyword would be a bit misleading... – David Oct 08 '14 at 13:16
  • @David Well, abstract members require `override`... and they're just signatures :D – Matías Fidemraizer Oct 08 '14 at 13:17
  • @MatíasFidemraizer: True, though still semantically different. They're still overriding functionality of a base class rather than implementing an interface. The difference is that the base class requires the override rather than merely offers it. (Since, as you say, there's no default implementation.) – David Oct 08 '14 at 13:18
  • @David Yeah, I know... I was just throwing garbage haha ;) – Matías Fidemraizer Oct 08 '14 at 13:26
  • 1
    FWIW: ReSharper shows a little Icon in front of members that implement interface members. There probably are other add ins, too. But no "plain" c#/.Net way. – Corak Oct 08 '14 at 13:38

3 Answers3

2

However, to my dismay it seems that (in VS2010 / .NET4) it is not possible to use override when implementing an interface method.

That's because interface methods aren't overridden, they're implemented. It's a seemingly trivial semantic difference, but when we're talking about the use of language semantics are pretty important.

but the overridekeyword would have served as a good simple documentation and check that the interface methods are actually these that are marked as override

Wouldn't it be a bit misleading? override implies that there's a base class definition being, well, overridden. The MSDN documentation defines it as:

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Interfaces aren't inherited, they're implemented. Again, just semantics, but a pretty important distinction. A class may implement multiple interfaces, the same method may be applied to multiple interface definitions, etc.

Overriding inherited behavior implies:

  • There is inherited behavior (either with a default implementation in the case of virtual or without in the case of abstract), keeping in mind that C# is a single-inheritance language
  • The implementation is being overridden (which carries specific distinctions in an inheritance model when the parent class internally invokes that member)

These conditions don't apply to interfaces.

So, when looking at a class implementation, is there any way other than a // comment to indicate that this method implements the method of a certain interface?

Actually, yes. You can explicitly implement an interface. Something like this:

interface IDimensions 
{
   float Length();
   float Width();
}

class Box : IDimensions
{
    public float IDimensions.Length() 
    {
        // implementation
    }

    public float IDimensions.Width() 
    {
        // implementation
    }
}
David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    Upvote for mentioning explicit implementation - this seems to be the convention the asker was looking for (and I was looking for). Should drop the "public" access modifier, though - apparently it gets forced by the explicit implementation notation. – Tydaeus May 14 '18 at 21:56
  • Upvote. Never saw that "explicit implementation" feature before. An absolutely useful feature for documentation and enforcing overriding. Unfortunately it requires calls to the method to be casted to the interface: `((IDimensions) this).Length()` – Andi Jan 23 '20 at 11:48
0

Suppose you have these types:

interface ISampleInterface
{
    void Method();
}

class A : ISampleInterface
{
    public void Method()
    {
    }
}

class B : A, ISampleInterface
{
    void ISampleInterface.Method()
    {
    }
}

class C : A, ISampleInterface
{
    public new void Method()
    {
    }
}

and use them this way:

ISampleInterface a = new A();
ISampleInterface b = new B();
ISampleInterface c = new C();

a.Method(); // calls A.Method
b.Method(); // calls explicit ISampleInterface.Method
((B)b).Method(); // calls A.Method
c.Method(); // calls C.Method
((A)c).Method(); // calls A.Method

Looks like it is hard to define, which of implementations of Method could be marked as override.

Dennis
  • 37,026
  • 10
  • 82
  • 150
0

I believe that everything about your concern is summarized by this sentence:

but the overridekeyword would have served as a good simple documentation and check that the interface methods are actually these that are marked as override.

Think about what's an interface, and what's an implementer. A class may or may not implement an interface, and can still implement a method with the same signature as an interface. What an interface does is the job of ensuring that some class has the required members to fullfil a contract.

For example, a class Calculator may implement ICalculator and Calculator implements Addition(int, int). But Calculator couldn't implement ICalculator and it could perform an Addition(int, int) anyway.

How do you distinguish both cases? When to use override or not.

Another point: it's nice to implement a class, and fulfill an interface, and stop fulfilling it by just removing it from the class signature after the inheritance colon.

In the other hand, think that the documentation you're looking for is the compiler error telling you that Calculator implements interface ICalculator but it doesn't declare and implement one or more members defined by ICalculator. If the code compiles, you shouldn't care about if a member is of some or other interface. You know that some members are implementations of some interface, because your Calculator signature would look like this: public class Calculator : ICalculator.

Also, there's the case where a implementation member implements it to fulfill more than an interface. What's overriding your implementation? Isn't this more confusing than avoiding override keyword?

Simon
  • 13,173
  • 14
  • 66
  • 90
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206