An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body (like Benj Nunez said). As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between Interface and Abstract class is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement some kind of multiple inheritance -> a class cannot inherit an interface but it can implement it -> so it can implement multiple interfaces, but that is not exactly multiple inheritance :)
Abstract class is a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared as abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes. No fields can be defined in interfaces, they are forced to be properties. As long you don't need to implement fields in interfaces, they are faster than abstract classes, especially on structs. If you need to access a lot of fields (from outside the class/struct), the best choice would be an abstract class, because an abstract class can have fields and constrants defined. On Nov 13, 10:52 am, "It's time to do something" <[email protected]> wrote: > Hi, can anyone here give me some ideas on how important interface is > (in C#) and what it can do? > I've learned that it can be used as polymorphism, but I don't > understand why in some cases we don't use polymorphism instead ? And > when do we use interface ? When do we use polymorphism instead ? > Thank you so much ! I'm just a newbie at all. Hope someone will help > me !
