Auberon
Auberon

Reputation: 715

Why can't I override my interface methods?

Let's say I have an interface as follows.

interface CardHolder : IEnumerable<Card>
{
    /// <summary> ...
    void PutCard(Card card);

    /// <summary> ...
    void PutCards(Card[] card);

    /// Some more methods...
}

I implement it as follows.

public class ArrayCardHolder : CardHolder
{
    private Card[] _cards;
    private int _size = 0;

    public ArrayCardHolder(int capacity)
    {
        _cards = new Card[capacity];
    }

    public void PutCard(Card card)
    {
        if (IsFull())
            throw new Exception("This CardHolder is full. Capacity: " + Capacity());

        _cards[_size++] = card;
    }

    public void PutCards(Card[] cards)
    {
        if (_size + cards.Length > _cards.Length)
            throw new Exception("Adding the Cards would exceed this CardHolder its capacity. Capacity: " + Capacity());

        for (int index = 0; index < cards.Length; index++)
            _cards[_size++] = cards[index];
    }
    public IEnumerator<Card> GetEnumerator()
    {
        for (int index = 0; index < _size; index++)
            yield return _cards[index];
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

    ///More methods.

}

Why can I not use the override keyword in my ArrayCardHolder (e.g. public void override PutCard(Card card) { ///implementation } to indicate that the method implements (i.e. overrides) the interface? In that case, the project will refuse to build.

Why does it work however when overriding ToString()? And why doesn't it work when implementing CompareTo(T t) from IComparable<T>?

What should I use instead? I'm worried that the documentation from the interface will not apply to my implementing methods. Such is the case in Java when the @Override annotation is used.

Upvotes: 15

Views: 59592

Answers (7)

Fitri Halim
Fitri Halim

Reputation: 739

I understand your concern, so I would like to suggest to use Explicit Interface Implementation.

This will help to demonstrate that the function declared in the function is to implement the interface, and any changes in the interface will cause the compiler to complain the implementing class to adopt the changes.

public class ArrayCardHolder : ICardHolder
{

    void ICardHolder.PutCard(Card card)
    {
        // Body
    }

    void ICardHolder.PutCards(Card[] cards)
    {
        // Body
    }
    
    ///More methods.

}

This way both methods are explicitly implementing the interface from CardHolder.

But the drawback from this method is that you cannot call this two methods from the object of Array cardholder, but you can only call it from the CardHolder interface itself. Please read the documentation to understand it more.

Upvotes: 0

Robin Davies
Robin Davies

Reputation: 7817

As mentioned abundantly, override applies to virtual and abstract method implementations, not interface method implementations.

The annoying thing about all of this is that there is no way to generate warnings when a method that used to implement an interface method becomes orphaned.

Interestingly, java tooling has an option to allow @override on interface methods, so that errors/warnings will be generated when an interface method becomes orphaned.

The question, from a language implementation point of view would be whether an override of a interfacement method would be virtual or not. I suppose "override virtual" would be an option. Or "override abstract".

https://github.com/dotnet/csharplang/issues/3510

So technically, the answer to WHY is not "because you can't", but something deeper and more sinister; namely: because the C# language spec is wrong. Just saying. :-P

Upvotes: 4

Mike Robinson
Mike Robinson

Reputation: 8945

An "interface" is a description of "what the public-facing face of some particular programming-thing must look like."

Any "concrete implementation" of that interface must do (at least) everything that the interface called for. (How it "gets 'er done" is entirely up to the implementor.)

So now, let's say that you've got two classes, Dad and Teenager, both of whom implement the same interface. But Teenager (although obviously a descendent of Dad) wants to do one of those things ... say, play_music ... a little bit differently. Teenager can override Dad's method.

So long as Teenager's implementation continues to conform to the strictures of the interface, our teen can play his music just as loudly as he likes.

Upvotes: 2

Zein Makki
Zein Makki

Reputation: 30022

.ToString is a virtual method in a base class Object . That's why you can override it. But you don't override an interface method, you implement it.

If you want to provide a default implementation and maybe override it in a class, then you need to use a base class with a virtual method. So :

Interface method: You must provide implementation in the class.

Abstract method: You must provide implementation in the derived class.

virtual method: you may provide implementation in the derived class or keep the default implementation. (or mix both by calling base.MethodName(); then providing additional code)

Example:

public interface IContract
{
    // It is a must to implement this method in classes
    void MustImplement();
}

public abstract class BaseClass
{
    // Just signature, No implementation. It is a must to implement.
    public abstract void IAmAbstract();

    public virtual void IAmVirtual()
    {
        Console.WriteLine("I provide default implementation");
    }
}

public class DerivedClass : BaseClass, IContract
{
    public override void IAmAbstract()
    {
        Console.WriteLine("provides Abstract Method implementation In Derived Class");
    }

    // It is optional to override this method
    public override void IAmVirtual()
    {
        // class default implementation
        base.IAmVirtual();

        Console.WriteLine("provides Additional virtual Method implementation In Derived Class");
    }

    public void MustImplement()
    {
        Console.WriteLine("provides Interface Method implementation In Derived Class");
    }
}

Upvotes: 1

Saket Choubey
Saket Choubey

Reputation: 916

You can override any method of base type if it is marked as virtual. For example you can override ToString() method as it is marked virtual in object class. And object is the base type in .Net.

enter image description here

 public override string ToString()
    {
        return base.ToString();
    }

Interfaces are implemented because they done have any implementation, hence there is nothing to override. For Example IComparable is an interface with CompateTo() method, It has no implementation, Hence you implement it in the class inheriting this interface.

enter image description here

public int CompareTo(object obj)
    {
        throw new NotImplementedException();
    }

I hope I made it clear.

Upvotes: 4

Camilo Terevinto
Camilo Terevinto

Reputation: 32068

Interface's methods are not overriden, they are implemented. You are confused with abstract/virtual methods which can be overriden.

Example:

public interface IFoo    
{
    void DoA();
}

public abstract class BaseFoo : IFoo
{
    public void DoA() { } // *this HAS to be implemented*
    public virtual void DoB() { } 
}

public abstract class MyFoo : BaseFoo
{
    // *this CAN be implemented, which would override the default implementation*
    public override void DoB() { } 
}

As others metioned, ToString is a virtual method of the base class object, that is why you can override it.

Upvotes: 26

driis
driis

Reputation: 164291

You are not overriding methods, you are implementing members of an interface.

In C#, override is only used when you are overriding an existing implementation. A case of this is ToString(). The method you are overriding must be marked virtual on the base class.

Upvotes: 1

Related Questions