So I'm having a bit of a tough time understanding Interfaces. On page 199 we have an example:
Cow myCow = new Cow();
Chicken myChicken = new Chicken();
consumeInterface = myCow;
consumeInterface = myChicken;
And i wondered to myself -- why would i want to do any of this? So I asked for help from a C++ programmer.
Can someone please let me know if i understand this correctly now?
- An interface is a contract - by implementing an interface, i agree to implement all the functions in that interface for my class.
- Interfaces in C# are a way to do multiple inheritance that's available in C++
- By assigning an object reference (as above) to an interface variable type, I'm basically using polymorphism like normal, but with C# i can't have multiple inheritances, so i need to do it with an interface.
And that's pretty much it, right? If anyone can shed further light on this, i'd greatly appreciate it.
As much as I enjoy Wrox books, i think the chapters devoted to OOP, polymorphism and Interfaces lack a lot as far as making the reader understand. The how is explained, but not the why...