0

Look at this code.

 public abstract class Customer
 {
    public abstract void Print();
 }

class Program : Customer
{
    public override void Print()
    {
        Console.WriteLine("Print Method");
    }
}

When we implement an abstract method of abstract class we use override keyword as shown above.

Now look at this code.

 public interface ICustomer
 {
    void Print();
 }

class Program : ICustomer
{
    public void Print()
    {
        Console.WriteLine("Print Method");
    }
}

When we implement a method of an interface we don't use override keyword.Why?

gamerdev
  • 65
  • 6
  • 2
    Because that's how this language feature was designed. – FCin Feb 01 '18 at 14:24
  • see [here](https://stackoverflow.com/questions/747517/interfaces-vs-abstract-classes) – Badiparmagi Feb 01 '18 at 14:27
  • 3
    Why we use `=` to assign a value? Why we use `& ` to perform a binary AND? Why we use `||` to perform a boolean OR? – Gusman Feb 01 '18 at 14:28
  • I am asking this question because interface members are abstract by default. So for implementing them shouldn't we use override like we do for abstract classes. – gamerdev Feb 01 '18 at 15:57

1 Answers1

2

For an interface, there is nothing to override. There is no implementation yet. The CLR doesn't have to walk the class hierarchy to find the class with the appropriate implementation, there is just one.

For abstract methods, there already is an implementation (or definition in the class), and that implementation has to be overriden. That is how the language has been defined.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Interfaces *might* be getting default implementations "soon". https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md – Bradley Uffner Feb 01 '18 at 14:31
  • But I didn't provide implementation for the abstract method in the class. The method is abstract so what's there to override. – gamerdev Feb 01 '18 at 15:53
  • The method is abstract. I guess the current rule is easier from a compiler implementation point of view. – Patrick Hofman Feb 01 '18 at 15:54
  • Also interface members are abstract by default. So shouldn't we write override for implementing them. – gamerdev Feb 01 '18 at 15:58