Why can't I override my interface methods?

33,761

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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.

Share:
33,761
Auberon
Author by

Auberon

Computer Science Student at the KULAK university, Belgium.

Updated on July 09, 2022

Comments

  • Auberon
    Auberon almost 2 years

    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.

  • Camilo Terevinto
    Camilo Terevinto almost 8 years
    "That's why you can't override it." Don't you mean "thats why you can override it"?
  • Zein Makki
    Zein Makki almost 8 years
    @cFrozenDeath sorry, a typo.
  • adv12
    adv12 almost 8 years
    Similarly: "override and interface method"--don't you mean "override an interface method"?
  • Zein Makki
    Zein Makki almost 8 years
    @adv12 fixed also.