What is the difference between an interface and a class, and why I should use an interface when I can implement the methods directly in the class?
Interfaces are excellent when you want to create something like it:
using System;
namespace MyInterfaceExample
{
public interface IMyLogInterface
{
//I want to have a specific method that I'll use in MyLogClass
void WriteLog();
}
public class MyClass : IMyLogInterface
{
public void WriteLog()
{
Console.Write("MyClass was Logged");
}
}
public class MyOtherClass : IMyLogInterface
{
public void WriteLog()
{
Console.Write("MyOtherClass was Logged");
Console.Write("And I Logged it different, than MyClass");
}
}
public class MyLogClass
{
//I created a WriteLog method where I can pass as a parameter any object that implements IMyLogInterface.
public static void WriteLog(IMyLogInterface myLogObject)
{
myLogObject.WriteLog(); //So I can use WriteLog here.
}
}
public class MyMainClass
{
public void DoSomething()
{
MyClass aClass = new MyClass();
MyOtherClass otherClass = new MyOtherClass();
MyLogClass.WriteLog(aClass);//MyClass can log, and have his own implementation
MyLogClass.WriteLog(otherClass); //As MyOtherClass also have his own implementation on how to log.
}
}
}
In my example, I could be a developer who writes MyLogClass
, and the other developers, could create their classes, and when they wanted to log, they implement the interface IMyLogInterface
. It is as they were asking me what they need to implement to use WriteLog()
method in MyLogClass
. The answer they will find in the interface.
One reason I use interfaces is because it increases the flexibility of the code. Let's say we got a method that takes an object of class type Account as parameter, such as:
public void DoSomething(Account account) {
// Do awesome stuff here.
}
The problem with this, is that the method parameter is fixed towards an implementation of an account. This is fine if you would never need any other type of account. Take this example, which instead uses an account interface as parameter.
public void DoSomething(IAccount account) {
// Do awesome stuff here.
}
This solution is not fixed towards an implementation, which means that I can pass it a SuperSavingsAccount or a ExclusiveAccount (both implementing the IAccount interface) and get different behavior for each implemented account.
Interfaces are contracts that implementers must follow. Abstract classes allow contracts plus shared implementations - something that Interfaces cannot have. Classes can implement and inherit multiple interfaces. Classes can only extend a single abstract class.
Why Interface
- You don't have default or shared code implementation
- You want to share data contracts (web services, SOA)
- You have different implementations for each interface implementer (
IDbCommand
hasSqlCommand
andOracleCommand
which implement the interface in specific ways) - You want to support multiple inheritance.
Why Abstract
- You have default or shared code implementation
- You want to minimize code duplication
- You want to easily support versioning