Home > Net >  Which is the difference beetwen implement a object of interface type or object of class type?
Which is the difference beetwen implement a object of interface type or object of class type?

Time:10-28

I have seen is possible to instantiate an object who implements an interface instead of a class. I´d like to know what are the benefits of this. I attach an example : C# example

CodePudding user response:

Interface just tells the program, that this object it just received has these methods defined. So you can have multiple different classes implement the same interface. And when a method accepts a given interface, then it can accept all the classes that implement the interface.

By instansiating a class into interface, you are saying, that whatever following code does, it cares only about the methods stated in the interface.

If you instansiate into class type, you are saying you want only this spesific type and none else.

  • Related