Monday, March 9, 2009

Interfaces over Abstract Myth

Why Use Interfaces?

As a word of closing, I would describe the advantages and importance of using the interfaces provided by C#; they are as follows:

  1. To allow a class to inherit multiple behaviors from multiple interfaces.
  2. To Avoid Name Ambiguity between the methods of the different classes as was in the use of multiple inheritance in C++.
  3. To Combine Two Or More Interfaces such that a class need only implement the combined result. The example code for this is provided as a source file to be downloaded.
  4. To allow Name hiding. Name hiding is the ability to hide an inherited member name from any code outside the derived class.






Interface cannot be instantiated

Same as Interface


Interface cannot contain concrete methods. But still there is no need to declare those methods as ‘abstract’ as in Abstract classes.

Abstract can have both Concrete & Abstract methods.


An ABSTRACT function does not have any implementation, and it must be overridden in any non-abstract derived class, using OVERRIDE keyword.


Note : C# does not support the “= 0” syntax to declare abstract functions.


Do not support Inheritance

Supports Inheritance


Cannot contain Fields (Member Vairables) & Constructor.


Otherwise – Methods, Properties, Events & indexers are allowed.


Can contain anything.


Not allowed to specify access modifiers.


By default they are public and cannot be virtual or static.

No Problems


An interface definition is also not allowed to contain operator overloads, though that’s not because there is any problem in principle with declaring them— there isn’t; it is because interfaces are usually intended to be public contracts, and having operator overloads would cause some incompatibility problems with other .NET languages, such as Visual Basic .NET, which do not support operator overloading.



No Problems





No comments:

Post a Comment