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(); } }
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.
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.
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”.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With