2

A lot of my colleagues mixed these words - interface implementation and interface inheritance. What is difference between these words (in C#)?

For me, according MSDN, correct is to say "class implements interface", therefore not "class inherits from interface."

2
  • Implementation is quite straightforward: It implements. An interface can inherit, but not implement. Therefore it's not the same thing Commented May 16, 2016 at 19:47
  • 1
    Inheritance usually makes you richer. Interface inheritance makes you poorer. You inherited the need to implement the interface. Commented May 16, 2016 at 19:57

3 Answers 3

3

Interface implementation

A type (a class) that implements an interface. For sample:

public interface IOperator { int Operation(int a, int b); } public class SumOperator : IOperator { public int Operation(int a, int b) { return a + b; } } public class DivisionOperator : IOperator { public int Operation(int a, int b) { return a / b; } } 

In this case, SumOperator and DivisionOperator are implementations of IOperator interface.

Interface inheritance

The structure of the interfaces (inheriths interfaces)

public interface IOperator { int Operation(int a, int b); } public interface IOppositeOperator : IOperator { int OppositeOperation(int a, int b); } public class SumOppositeOperator : IOppositeOperator { public int Operation(int a, int b) { return a + b; } public int OppositeOperation(int a, int b) { return a - b; } } 

In this case, the IOpositeOperator inherits IOperator, so a class which implements the IOpositeOperator should also implements the elements defined in its structure (including IOperator , given it inherits). The concrete type for it is the SumOpositeOperator sample class.

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

3 Comments

Since interface cannot implement intereface it is not correct to say IOppositeOperator implements IOperator. The correct definition is : IOppositeOperator inherits IOperator
@user2457382 sorry. My english skills is the perfect. Thank you for suggestion!
I just concluded what you wrote :)
0

You are right, we generally think of classes as implementing interfaces, not inheriting interfaces; however, it is a relatively minor mistake to make, and everyone understands what is meant by "my class inherits such and such interface".

Also, it is entirely correct to speak of interface inheritance in various scenarios, for example:

  1. When an interface extends one or more other interfaces, we say that the interface inherits from those interfaces.
  2. When class X extends class Y and class Y implements interface I, in which case we may say that class X is inheriting interface I via Y.
  3. When an abstract class "implements" an interface without providing any implementation for any of the methods of the interface. (Which is perfectly valid because it is an abstract class.) In this case it is not correct to say that it implements the interface, because it doesn't really.

Comments

0

Interface inheritance and interface implementation are not the same thing.

A class implements an interface by declaring that it implements an interface and then containing the required members to to implement that interface.

Interface inheritance refers to an interface inheriting from one or more other interfaces. A notable difference from class inheritance is that while a class can only directly inherit from one other class, an interface can inherit from multiple interfaces.

In this example, IInterfaceC does not define any methods of its own, but it includes the methods defined in IInterfaceA and IInterfaceB.

public interface IInterfaceA { void MethodA(); } public interface IInterfaceB { void MethodB(); } public interface IInterfaceC : IInterfaceA, IInterfaceB {} 

(If your coworkers use the terms "interface implementation" and "interface inheritance" interchangeably, correct them. They'll appreciate it.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.