0

I have been reading some code theory related to multiple inheritance and interfaces. It said in all the places that interface is a class without implementation.

1) What is the use of not having implemented methods/functions in an interface? Is it to support multiple implementations of the same method in different classes that inherit an interface?

2) Most of the code samples out there seem to show void interface methods. Are interface functions/methods always void?

aspiring
  • 1,557
  • 2
  • 20
  • 43
  • You should try to search a little before posting a question: [here](http://stackoverflow.com/questions/3844200/interfaces-in-c-sharp) – bash.d Feb 15 '13 at 11:37
  • 1
    (1) Pretty much, yes. (2) Not necessarily; you can have non-void methods, properties, events... – Rawling Feb 15 '13 at 11:38
  • Maybe a little background: C# does not have multiple inheritance like c. In C you could have had a base class with some methods, 2 (or more) child classes of the base class and then a "grand"child class which inheritance from both child classes. This is commonly known as "diamond problem". Should the grandchild behave like one of the childs and of which one? java and c# try to avoid problems from this constellation using an "abstract" class, which is the interface. you can implement some interfaces, but cannot inheritance from more then one class. – Offler Feb 15 '13 at 11:45
  • @Offler, I had a great conversation here in SO on MI :) Appreciate your input though. As a summary to your comment's latter section, can I say this `Class D: A, IB, IC`? (IB, IC are two different interfaces, so this class D is inheriting from Class A and two interfaces/abstract classes) – aspiring Feb 15 '13 at 11:51
  • If you do something like 'class D: A, IB, IC' you inherit from class A. If A has non abstract public methods you can call them. With the IB you only say: D contains the implementation for methods which are declared in IB, but have no code in IB. D implements the code which is neccessary to act as IB. Or in other words: you truly inherite from A, which declares and defines methods which could be used; IB only shows which structure is needed, that there needs to be something with this name if you implement the interface in a class. – Offler Feb 18 '13 at 07:03

3 Answers3

1

1) What is the use of not having implemented methods/functions in an interface? Is it to support multiple implementations of the same method in different classes that inherit an interface?

YES.

2) Most of the code samples out there seem to show void interface methods. Are interface functions/methods always void?

No. Not at all. It can return anything.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thanks. Well in first case, I can still write a parent class and inherit its one particular method, and implement it differently in multiple child classes.. I managed to do so. So what is the advantage of putting these methods in an interface (other than trying to have a workaround of multiple inheritance) ? :) – aspiring Feb 15 '13 at 11:43
  • @aspiring, well your question should be Why use an interface ? , you have already mentioned one of the reason in your comment, interface actually defines a contract and useful with polymorphism. You need to read more about the concept, this may be a good start http://www.cs.utah.edu/~germain/PPS/Topics/interfaces.html – Habib Feb 15 '13 at 11:55
  • Thank you :) helpful, all the help and articles. – aspiring Feb 15 '13 at 13:28
1

For 1) Yes, your idea is correct. Assume, you have an interface which just has some method makeSound. Now you can implement that interface in some Guitar class and some Drums class. You only have to know in your program that you have some object which implements makeSound so you can call that method. No need to know, what the actual output/sound will be.

For 2) No, an interface can contain methods with any signature.

Dominik Sandjaja
  • 6,326
  • 6
  • 52
  • 77
  • So you mean similar to `Draw()` method in `Graphics` class? But then `Guitar` must give guitar sound, and so is `Drums`. To say when implemented that `makeSound` method, it will take the sound of the child class implementation? if it was `makeShape`, it will take the shape of the child class? – aspiring Feb 15 '13 at 11:45
  • 1
    It will take the best-fitting implementation. If you `override` an implementation, that one will be taken. Because `Guitar` and `Drums` do not inherit from each other, their respective implementation will be used. – Dominik Sandjaja Feb 15 '13 at 11:52
  • 2
    @aspiring don't think of interface implementations as "child classes". The interface is just sort of a promise that an object implementing that interface will have a method named X with signature Y. In DaDaDoms example Interface.makeSound would actually be Guitar.makeSound, if your object is a Guitar. And it would be Drums.makeSound if the object were of type Drums. – Dirk Trilsbeek Feb 15 '13 at 12:01
1

To question 1: yes, that is one reason to use interfaces. Interfaces are often used as an API for a component. The actual implementation can be unknown to the consumer, supporting loose coupling and testability through unit tests.

Regarding question 2: no, interface methods can have the same method signatures as class methods.

Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23
  • Thanks. Can you please provide an example of the latter reason: *"The actual implementation can be unknown to the consumer, supporting loose coupling and testability through unit tests."*? – aspiring Feb 15 '13 at 11:44
  • loose coupling is mostly used with Dependency Injection containers like Castle Windsor, Unity, NInject or Spring Framework. To get the general idea of DI (or Inversion of Control as a more general name for the pattern) you should look at a few tutorials, like this: http://joelabrahamsson.com/entry/inversion-of-control-introduction-with-examples-in-dotnet . Regarding testability: unit tests should test only a single unit, so a tested component that relies on (maybe a lot of) other components would be hard to test standalone. Using mocking frameworks or stubs you can make unit testing easier. – Dirk Trilsbeek Feb 15 '13 at 11:48
  • Well that goes beyond my hat :) I looked at [this tutorial](http://www.codeproject.com/Tips/389019/Why-Csharp-interface-inheritance-makes-sense-see-L) in terms of `interface inheritance` not on LinQ. However most of the articles state `classes implement interfaces` instead of `classes inherit from interfaces`. Similar to what you mentioned in one of your comments. So what's the correct norm? implements or inherits? If so, why would this article says inherits? – aspiring Feb 15 '13 at 13:28
  • interface inheritance is a concept where one interface inherits method declarations from another interface. Implementing such an interface is no different from implementing one that doesn't inherit any methods declarations. – Dirk Trilsbeek Feb 15 '13 at 17:15