Interfaces Versus Abstract Classes

The two types are similar in a number of ways, so it would be useful to know how to determine when you would want to use one technique or the other.
First the similarities: Both abstract classes and interfaces may contain members that can be inherited by a derived class. Neither interfaces nor abstract classes may be directly instantiated, but you can declare variables of these types. If you do, you can use polymorphism to assign objects that inherit from these types to variables of these types. In both cases, you can then use the members of these types through these variables, although you don ’ t have direct access to the other members of the derived object.
Now the differences: Derived classes may only inherit from a single base class, which means that only a single abstract class can be inherited directly (although it is possible for a chain of inheritance to include multiple abstract classes). Conversely, classes can use as many interfaces as they want, but this doesn ’ t make a massive difference — similar results can be achieved either way. It ’ s just that the interface way of doing things is slightly different.
Abstract classes may possess both abstract members (these have no code body and must be implemented in the derived class unless the derived class is itself abstract) and non – abstract members (these possess a code body, and can be virtual so that they may be overridden in the derived class). Interface members , conversely, must be implemented on the class that uses the interface — they do not possess code bodies. Moreover, interface members are by definition public (because they are intended for external use), but members of abstract classes may also be private (as long as they aren ’ t abstract), protected, internal, or protected internal (where protected internal members are accessible only from code within the application or from a derived class). In addition, interfaces can ’ t contain fields, constructors, destructors, static members, or constants.


You may also like...