I know that an interface does not have a body, just a method definition. But when should I use interfaces? If I provide someone a set of interfaces with no body, why would they feel a need to write the function body? Would they be better off writing their own abstract class with abstract methods in it?
The way I understand interfaces is that they could be useful when you are a part of a team. Suppose Team A writes some code for something and they wanted to see if a call to a method getRecords()
is done or not. This will help Team B to write the body of the interface provided to them and Team B has to keep the method name similar so that code of Team A runs.
As far as I can tell, interfaces don't appear to be useful for individual developers.
Maybe interfaces have more use when you are making something like API?
In languages such as Java and C# interfaces provide a means for a class to be have in a polymorphic manner. That is to say a class can satisfy more than one contract - it can behave as multiple different types, a class of one type can be substituted for another. In other languages this can also be provided by multiple inheritance, but there are various disadvantages to this approach. However, having a class behave as more than one type is not the most common motivation for using interfaces.
By programming to interfaces instead of classes you can also decouple your program from specific implementations. This makes it much easier to substitute one class implementation for another. This is particularly useful when writing unit tests where you may wish to swap some heavyweight class implementation with a lightweight mock object. If your program only expects an interface type, and both the heavyweight object and mock object implement said interface, then they are very easy to substitute.
Also, consider a simple Java example where I say have a program that displays pages of data on the screen. Initially I want it to get the data from a database or XML files. If I write my program so that it uses interfaces I can define an interface like so:
public interface PageDatasource {
public List<Page> getPages();
}
And use it like so:
PageDatasource datasource = // insert concrete PageDatasource implementation here
List<Pages> pages = datasource.getPages();
display(pages);
I can then write separate database and XML implementations that adhere to this interface:
public class DatabasePageDatasource implements PageDatasource {
public List<Page> getPages() {
// Database specific code
}
}
public class XmlPageDatasource implements PageDatasource {
public List<Page> getPages() {
// XML specific code
}
}
Because I used an interface I can now use either implementation - Database or XML - interchangeably without having to change the parts of my program that ask for the page data. The XML and Database implementations likely do completely different things but all my program cares about is that the object supplying the page data implements the PageDatasource
interface.