Wednesday, January 2, 2013

Abstract Class and Interfaces

Abstract Class:

      An abstract function does not have an implementation, and must be overridden in any non -abstract derived class.
      Obviously, an abstract function is automatically virtual (although you don’t need to supply the virtual keyword; doing so results in a syntax error).
      An abstract class cannot be instantiated i.e. One cannot create objects of an abstract class.
      If any class contains any abstract functions, that class is also abstract and must be declared as such:
                             abstract class Building
{
public abstract decimal CalculateHeatingCost();
// abstract method
}

Interfaces:

·         A C# interface provides a contract stated in terms of .NET functions.

·         The main benefit of using interfaces is that programmers can create situations where components in a program can be interchangeable. These will all implement the same interface, so no extra coding is needed to make this work. By using interfaces, the component will expose only certain public members that can be made use of.
      An interface can only contain declarations of methods, properties, indexers, and events.
      You can never instantiate an interface; it contains only the signatures of its members.
       An interface has neither constructor (how can you construct something that you can’t instantiate?) nor fields (because that would imply some internal implementation).
      An interface definition is also not allowed to contain operator overloads, 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.
      It is also not permitted to declare modifiers on the members in an interface definition. Interface members are always implicitly public, and cannot be declared as virtual or static.

Example IDisposable

If a class wants to declare publicly that it implements the Dispose() method, it must implement IDisposable — which in C# terms means that the class derives from IDisposable.
class SomeClass : IDisposable
{
// This class MUST contain an implementation of the
// IDisposable.Dispose() method, otherwise
// you get a compilation error.
public void Dispose()
{
// implementation of Dispose() method
}
// rest of class
}

Feature
Abstract Class
Interface
Methods
 An abstract class can have non-abstract Methods(concrete methods) but At least one abstract method
All methods are abstract
Fields and Constants
An abstract class can have fields and constants defined
No fields can be defined in interfaces
constructor
An abstract class can have constructor declaration
Interfaces cannot contain constructors
access modifiers
An abstract Class is allowed to have all access modifiers for all of its member declaration
While in interface we cannot declare any access modifier (including public) as all the members of interface are implicitly public.
Multiple inheritance
Not supported.
A class may inherit only one abstract class.
Supported.
A class may inherit several interfaces.
Component Versioning
By updating the base class all derived classes are automatically updated.
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.
Interfaces are immutable.
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.

No comments:

Post a Comment