Sunday, May 6, 2007

Interface vs Abstract Class

I frequently asked what is the difference between Interface and Abstract classes. The main difficulty is the choice of whether to design your functionality as an interface or an abstract class.


Interface
An interface is a reference type containing only abstract members. These can be events, indexers, methods or properties, but only the member declarations. A class implementing an interface must provide the implementation of the interface members. An interface cannot contain constants, constructors, data fields, destructors, static members or other interfaces. Interface member declarations are implicitly public.
An interface declaration may declare zero or more members.
All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, it is a compile-time error for an interface member to include any of the following modifiers: abstract, public, protected, internal, private, virtual, override, or static.



Abstract class
Like an interface, you cannot implement an instance of an abstract class, however you can implement methods, fields, and properties in the abstract class that can be used by the child class.
The abstract modifier is used to indicate that a class is incomplete and that it is intended to be used only as a base class. An abstract class differs from a non-abstract class is the following ways:
An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on an abstract class. While it is possible to have variables and values whose compile-time types are abstract, such variables and values will necessarily either be null or contain references to instances of non-abstract classes derived from the abstract types.
An abstract class is permitted (but not required) to contain abstract members.
An abstract class cannot be sealed.
When a non-abstract class is derived from an abstract class, the non-abstract class must include actual implementations of all inherited abstract members
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.


List of similarities and differences between an interface and an abstract class:
  • In practice,an interface is a good way to encapsulate a common idea for use by a number of possibly unrelated classes, while an abstract class is a good way to encapsulate a common idea for use by a number of related classes.
  • When you need to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
  • An Interface can only inherit from another Interface, when an abstract class can inherit from a class and one or more interfaces
  • An Interface cannot contain constructors or destructors, when abstract class can contain constructors or destructors.
  • An Interface can be inherited from by structures,when abstract class cannot be inherited from by structures.
  • An Interface can support multiple inheritance,when abstract class cannot support multiple inheritance.
  • Interfaces are used to define the peripheral abilities of a class;An abstract class defines the core identity of a class and there it is used for objects of the same type.
  • If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method; If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

3 comments:

Maor David-Pur said...

http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/05/09/Interface-vs-Abstract-Class.aspx

Anonymous said...

why are you saying that in Interface methods/variables, we cnt use public/abstract keywords.Its possible, right? Only private is disallowed

Parvez said...

Even protected is not allowed. But yes, public and abstract keywords are allowed. Member variables in an interface are implicitly public and static (in the sense that we do not need an object instance in the subclass to use it) but they would not give a compile error if they are declared so in the interface.