Friday, September 10, 2010

What is difference between interface and abstract class

In C# there is no multiple inheritance. You can only inherit from one base
class, however you can implement multiple interfaces. An abstract base class
can't be directly instantiated, but it can implement code an interface can
only define it. A class that inherits from an abstract base class can choose
to override members or not (unless they are abstract themselves). All
members of an interface must be implemented.

Much of the time a class derived from another class will have a "type of"
relationship with the base class for example:

abstract class Fruit

class Apple : Fruit

Often interfaces represent a set of capabilities for example:

interface Eatable
public void Chew();

class Apple : Fruit, Eatable
public void Chew()

Also it depends on how polymorphic your code is. If you have divergent
objects that can be acted on in a similar way with in a system, it makes
sense to define interfaces for example:

class Apple : Fruit, Eatable

class Cow : Eatable

It is unlikely that class Cow and class Apple would inherit from the same
base class but it is likely that they would both be acted upon in a similar

The following table compares the features of abstract classes and interfaces.

Abstract class
Derived classes exhaust their single base class inheritance option.
Classes can implement multiple interfaces without using up their base class option. But, there are no default implementations.

Cannot be instantiated except as part of subclasses. Only derived classes can call an abstract class constructor.
Cannot be instantiated.
Defines abstract member signatures which derived classes must implement. Otherwise, the derived class itself will be abstract.
Defines abstract member signatures—all of which—implementing classes must implement. Otherwise, a compiler error results.
New non-abstract members may be added that derived classes will inherit without breaking version compatibility.
Extending an interface with new members breaks version compatibility.
Optionally, provide default (virtual) member implementation.
All members are virtual and cannot provide implementations.
Can include data fields.
Cannot include data fields. However, abstract properties may be declared.

So finally:
Interface programming is called the Programming by Contract because the class which is implementing the interface has to implement all the functions of interface. Interfaces is the only way provide multiple inheritence is C#. A class can not be inherited by more than one classes but it can implement more than one interfaces simultaneously. This was a short descritption about the Abstract Classes and Interfaces. Now the real thing comes in to picture. That What to Use When? So following are the points to remeber duirng the usage of Abstract Classes and Interfaces-
1. Interfaces should be used when creating a standalone project which can be changed at anytime, use an interface in preference to an abstract class; because, interfaces offers more design flexibility.
2. Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance.
3. Use an interface to design a polymorphic hierarchy for value types.
4. Use an interface when an immutable contract is really required.
5. Use an abstract class to define a common base class for a family of types.
6. Use an abstract class to provide default behavior to your application.
7. Subclass only a base class in a hierarchy to which the class logically belongs.
8. If you add a new functionality in abstract class then derived class can easily implement without breaking the versioning functionality. But in case of Interfaces addition of new interface member breaks the versioning functionality.

Reference Link:

Solution By:Rajesh Rolen

Share This!

No comments:

Powered By Blogger · Designed By Seo Blogger Templates