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 :
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.