Why do we instantiate an object from an interface instead of a class?

I have seen many times an interface instance created from a class. Why does he use the interface in this sage? An instance of an interface is created only by itself using a derived class, and we can only access these interface members through this instance. How does this give an advantage? I'm so confused.

interface IPrint { void Print(); } class Sample : IPrint { public void Print() { Console.WriteLine("Print..."); } public void Sample() { Console.WriteLine("Sample..."); } } class Program { static void Main(string[] args) { IPrint print = new Sample(); print.Print(); } } 
+42
c # class interface derived-class
May 30 '13 at 9:20
source share
4 answers

Interfaces determine that a class MUST be able to do something. This means that you know that the object you are working on will do what you want to do. This allows you more freedom and benefits of OOP. This is a deep topic, but a very simple example:

 public interface IAnimal { string Speak(); } public class Dog : IAnimal { public string Speak() { return "Woof, woof"; } } public class Cat : IAnimal { public string Speak() { return "Meow"; } } public class Parrot : IAnimal { public string Speak() { return "Sqwark!"; } } 

Then you can use any animal that you like!

 class Program { static void Main(string[] args) { // Writes Woof, Woof IAnimal animal = new Dog(); Console.WriteLine(animal.Speak()); // Now writes Meow animal = new Cat(); Console.WriteLine(animal.Speak()); // Now writes Sqwark etc animal = new Parrot(); Console.WriteLine(animal.Speak()); } } 

It also allows you to go into things like Inversion Of Control , where you could take such an item and you could pass the dog, cat or parrot, and the method will always work, not knowing or caring about what kind of animal it is :

 public void ShoutLoud(IAnimal animal) { MessageBox.Show("Shout " + animal.Speak()); } 

This makes ShoutLoud a single testable because you can use a mock object rather than a real animal. This basically makes your code flexible and dynamic, rather than rigid and tightly coupled.

In addition, Matthew's question expands. In C #, you can only inherit one base class, but you can have multiple interfaces. So you could:

 public class Dog : IAnimal, IMammal, ICarnivor 

This allows you to have small interfaces (recommended) that then allow you to create, giving you maximum control over what the item can / should do.

+73
May 30 '13 at 9:34
source share

Using an interface in this way gives you the ability to create methods that use a standard interface template. So here you can have many printer classes that inherit from IPrinter

 class SamsungPrinter : IPrinter { // Stuff and interface members. } class SonyPrinter : IPrinter { // Stuff and interface members. } interface IPrinter { void Print(); } 

So, for each type of SamsungPrinter , SonyPrinter , etc. you can pre-process using something like

 public static void PreProcessAndPrint(IPrinter printer) { // Do pre-processing or something. printer.Print(); } 

You know, inheriting from IPrinter and using this type in the method parameters, which you can always use safely with the Print method for what is ever passed to the object.

Of course, there are many other uses for using interfaces. One example of their use is design patterns, in particular Factory and Strategy patterns. A description of which and examples can be found here .

Hope this helps.

+7
May 30 '13 at 9:31
source share

But how does this differ from, for example, using a base class with virtual methods?

You are all under the assumption that one programmer or one program writes the interface and classes, but this does not always have to be this way.

Perhaps you have a complete ready-made program that works with animals, and you developed it using:

 public abstract class Animal { public abstract string Speak(); } 

And then, someday you will download some awesome dll from nuget that shows pictures for animals. The class library contains a contract interface - "IAnimal":

 namespace AwesomeAnimalLibrary { public interface IAnimal { string AnimalName; } } 

The class library may also contain:

 namespace AwesomeAnimalLibrary { public class AnimalPhotos { [Byte] GetPhotos(IAnimal animal); } } 

What can you do now? Your base class Animal can implement the AwesomeAnimalLibrary IAnimal interface and what it is.

Do not assume that other people will use you in abstract base classes, but work together using interface contracts.

+1
Jun 01 '13 at 10:47 on
source share

An interface cannot have an instance because an interface implements only property or method signatures. An interface is just a pointer to an instance of some class:

 interface IExample { // method signature void MyMethod(); } public class MyClass : IExample { // method implementation public void MyMethod() { ConsoleWriteline("This is my method"); } } // interface pointing to instance of class IExample ie = new MyClass(); ie.MyMethod(); 
0
Apr 14 '17 at 9:52 on
source share



All Articles