Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between Interface and Class [duplicate]

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.

like image 670
Daniel Eugen Avatar asked Apr 07 '13 01:04

Daniel Eugen


People also ask

What is the difference between class and interface?

Differences between a Class and an Interface:A class can be instantiated i.e, objects of a class can be created. An Interface cannot be instantiated i.e, objects cannot be created. Classes does not support multiple inheritance. Interface supports multiple inheritance.

What is the difference between class and interface in SAP?

Main difference between a CLASS and an INTERFACE is: - Class has both definition and an implementation whereas Interface only has a definition. Interfaces are actually implemented via a Class. (Due to this way of implementing Interfaces, the ABAP Objects supports the concept of multiple inheritances.)

What is difference between class and interface in C#?

A class contains attributes, methods and events as its components. Interfaces contain only method prototypes, they don't provide an implementation of its methods, we can provide an implementation of its methods in our class, for that we can implement the interface. An interface is the source for polymorphism.

What is the difference between interface and multiple inheritance?

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.


2 Answers

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 a Giraffe is a class of animals. Inheritance expresses this relationship: an Giraffe is an Animal when Giraffe inherits from Animal. 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 is Object 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 implements ICollection, 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 a Truck may be a Vehicle and a CargoCarrier, you cannot express this in C#.

like image 181
Daniel A.A. Pelsmaeker Avatar answered Sep 25 '22 23:09

Daniel A.A. Pelsmaeker


Basically:

An interface provides a contract specifying how to talk to an object, but not the specifics of how that object handles that request (apart from parameters and return types etc)

And:

A class (especially an abstract class) provides both the information on how to talk to an object, but in some cases the actual implementation (think overriding a method of a base class).

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:

public interface ILog
{
   void WriteMessage(string message);
}

public class ConsoleLogger : ILog
{
   public void WriteMessage(string message)
   {
      Console.WriteLine(message);
   }
}

public class MessageBoxLogger : ILog
{
   public void WriteMessage(string message)
   {
      MessageBox.Show(message);
   }
}

public void DoSomethingInteresting(ILog logger)
{
   logger.WriteMessage("I'm doing something interesting!");
}

Because both ConsoleLogger and MessageBoxLogger implement the ILog interface (the WriteMessage method, any part of code can take an ILog 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 or MessageBoxLogger could be supplied to DoSomethingInteresting it doesn't matter at all because ILog "knows" how to talk to that object.

like image 40
Clint Avatar answered Sep 22 '22 23:09

Clint