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?

4
  • 2
    Because that's how this language feature was designed. Commented Feb 1, 2018 at 14:24
  • see here Commented Feb 1, 2018 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? Commented Feb 1, 2018 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. Commented Feb 1, 2018 at 15:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Interfaces might be getting default implementations "soon". github.com/dotnet/csharplang/blob/master/proposals/…
But I didn't provide implementation for the abstract method in the class. The method is abstract so what's there to override.
The method is abstract. I guess the current rule is easier from a compiler implementation point of view.
Also interface members are abstract by default. So shouldn't we write override for implementing them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.