Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we do create object instance from Interface instead of Class?

I have seen an Interface instance being generated from a class many times. Why do we use interface this way? An interface instance is created only itself with the help of the derived class and we can access only 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();     } } 
like image 599
user2282567 Avatar asked May 30 '13 09:05

user2282567


People also ask

Why do we use interface instead of class?

An interface defines common functionality across unrelated classes. For example, all sorts of classes that look nothing like each other may have the need to safely get rid of the resources they use. The IDisposable interface (the name for it in . NET) defines a Dispose() method, which classes then implement.

Why do we create object of interface?

Interface is basically a complete abstract class. That means Interface only have deceleration of method not their implementation. So if we don't have any implementation of a method then that means if we create object of that interface and call that method it compile nothing as there is no code to compile.

Can we create instance of an interface?

No, you cannot instantiate an interface. Generally, it contains abstract methods (except default and static methods introduced in Java8), which are incomplete. Still if you try to instantiate an interface, a compile time error will be generated saying “MyInterface is abstract; cannot be instantiated”.

Why We Need interface instead of class and what we are achieving from interface?

Interface is nothing but its a guide line for the new implementation it provides some instructions for new implementation and categorize the functionality of the object . In details like if we create an interface then we create an instruction for the implementation .


1 Answers

Interfaces define that a class MUST be able to do something. This means that you know the object being worked on will do what you want to be able to do. It allows you greater freedom and is one of the advantages of OOP. This is a deep topic but a very basic example would be this:

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 could use any animal 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());     } } 

This also allows you to then get into things like Inversion Of Control where you would take an item in like this and you could pass a dog, cat or parrot and the method would always work, not knowing or caring which animal it was:

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

This then makes ShoutLoud unit testable because you could use a mock object rather than a real animal. It basically makes your code flexible and dynamic rather than rigid and tightly coupled.

Also, expanding on Matthew's question. In C# you can only inherit from one base class but you can have multiple interfaces. So, you could have:

public class Dog : IAnimal, IMammal, ICarnivor 

This allows you to have small interfaces (recommended) that then allow you to build up so giving maximum control over what an item can / must do.

like image 77
Belogix Avatar answered Sep 21 '22 00:09

Belogix