Interfaces... What's the deal?
This is a newbie question. I do not understand the whole idea of the interface thing.
It is my understanding that an interface is basically just an abstract class with abstract methods that all have to be implemented in the class that implements them. So, if you have to implement all of these properties and methods on the class implementing the interface anyway, what is the point of having an interface, other that the fact that the new properties and methods will show up in your auto-complete in Visual Studios. If this is all an interface is, how does that replace multiple inheritance?
Any insight would be appreciated.
-agartee
MykoninePosted Dec 7, 2004, 9:57 PM
adam_garteePosted Dec 7, 2004, 2:06 PM
jsheplerPosted Dec 4, 2004, 3:50 PM
jsheplerPosted Dec 4, 2004, 11:36 AM
... ArrayList items = new ArrayList(); // some code that populates the list foobar(items); ... void foobar(IEnumerable list) { // code that works with the list as an IEnumerable object }foobar() doesn't care what class of object is passed to it - you can pass it an Array object, an ArrayList object, a Cache object, ControlCollection object, etc. Any object that implements IEnumerable can be passed to foobar(). A hypothetical example: You have been givin the project of writing a class library to deal with animal behaviors. One of the behaviors is Speak(). Each animal that can actually speak will implement that method in a different way (dogs bark, lions roar, etc). Granted, you can just define the method for each class that can speak and not include it for any that don't, but the developers that will use your class library will have no easy way to determine if any particular class of animal CAN speak. If you create an interface that defines the Speak() behavior, not only will your developers be able to determine if a class can speak, they can expect all classes that implement the ISpeakable interface to implement the Speak() method specifically.bilnaadPosted Dec 4, 2004, 10:42 AM
adam_garteePosted Dec 3, 2004, 2:40 PM
class Car { public void Brake() { Console.WriteLine("Brake!"); } void HandBrake() { Console.WriteLine("HandBrake!"); } } class Interfaces { public static void Main() { Car car = new Car(); car.Brake(); car.HandBrake(); Console.Read(); } }Other that being able to name the methods the same thing and referring to them by the interface, the same thing is done. I suppose a more complicated, real-world example would help me understand a bit better. Every tutorial I have read says basically the same thing you did, nobrainer, but I still don't understand why you would want to use an interface, and more importantly, how this replace multiple inheritance. I have no programming training (I'm just figuring it out as I go), so maybe I'm just missing some fundamental concept that you all learned in college. Interfaces just look to me like a "don't forget to put this method in, mr. programmer" helper in Visual Studios. I highly doubt this is true, but I just can't seem to wrap my head around this concept.bilnaadPosted Dec 3, 2004, 12:31 PM