This question already has an answer here:
As the title states i want to know the difference between using the class like this
public class Account
{
public string Username { get; set; }
public string Password { get; set; }
}
and using and Interface like this
public class Account : IAccount
{
public string Username { get; set; }
public string Password { get; set; }
}
public interface IAccount
{
string Username { get; set; }
string Password { get; set; }
}
I am really confused as i find interface is useless as all i can do with it can be done only using Class, Hence, i need someone to clarify things for me.
An interface is a contract: it specifies what members (methods and properties) a class implementing the interface must have. But because it is only a contract, it has no implementations for any of its members. A class can implement zero, one or multiple interfaces.
In contrast: a class is a... well... class of objects (like in taxonomy). For example, an
Animal
is a class of living things, and aGiraffe
is a class of animals. Inheritance expresses this relationship: anGiraffe
is anAnimal
whenGiraffe
inherits fromAnimal
. It can do anything an animal can do, and more. It can provide implementations for its members, and in .NET a class will inherit from exactly one other class (which isObject
unless specified otherwise).So, if you want to express that your class adheres to one or more contracts: use interfaces. However, you cannot provide an implementation. If you want to express that your class is something, extend a base class. In that case you can provide an implementation, but you can extend only one base class.
For a concrete example:
A linked list, an array list, a stack and a dictionary have something in common: they represent a collection of elements. But their implementations are completely different. The only thing they have in common is the contract they adhere to:
ICollection
. This means your classes can ask for a collection, any collection: anything that implementsICollection
, regardless of its implementation.On the other hand: a car, a motorcycle and a truck also have something in common: they are wheeled vehicles. But they have more in common than that: they all have a motor, they all spin their tires to go forward. Essentially, they are members of the
Vehicle
class of objects, and can share (part of) their implementation. However, while aTruck
may be aVehicle
and aCargoCarrier
, you cannot express this in C#.Basically:
And:
Interfaces allow you to define a common form of communicating between objects, without caring about the specifics of how they do the things.
An example would be logging:
Because both
ConsoleLogger
andMessageBoxLogger
implement theILog
interface (theWriteMessage
method, any part of code can take anILog
without ever needing to know what it actually does, they only care that it does something - in this case, writing a log message.So in the code above, either a
ConsoleLogger
orMessageBoxLogger
could be supplied toDoSomethingInteresting
it doesn't matter at all becauseILog
"knows" how to talk to that object.