BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
This is the forum to discuss the Wrox book Beginning Microsoft Visual C# 2008 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780470191354
You are currently viewing the BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other programmers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
So I'm having a bit of a tough time understanding Interfaces. On page 199 we have an example:
Code:
Cow myCow = new Cow();
Chicken myChicken = new Chicken();
IConsume consumeInterface;
consumeInterface = myCow;
consumeInterface.EatFood();
consumeInterface = myChicken;
consumeInterface.EatFood();
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...