Thursday, March 18, 2010

Abstract class vs Interface

What is an Abstract class?

                An abstract class only allows other classes to inherit from it and cannot be instantiated. When we create an abstract class, it should have one or more completed methods but at least one or more uncompleted methods and these must be preceded by the key word abstract. If all the methods of an abstract class are uncompleted then it is the same as an interface, but there is a restriction that it cannot make a class inherit from it, which means it can not work as a base class.

Define an Abstract Class
In the following code listing an abstract class named Product has been defined.
Listing 1
public abstract class Product
{
  //fields
  protected string id;
  protected double price;
  //properties
  public abstract string ID
  {
    get;
    set;
  }
 
  public abstract double Price
  {
    get;
    set;
 
  }
  public abstract double CalculatePrice();
}
 
Implementation

In the following code listing the abstract Class named Product has been implemented by the class implementAbstract.

Listing 4
public class implementAbstract: Product
{
  public override string ID
  {
    get
    {
      return id;
    }
    set
    {
      id = value;
    }
 
  }
  public override double Price
  {
    get
    {
      return price;
    }
    set
    {
      price = value;
    }
 
  }
  public override double CalculatePrice
  {
    return Price * 1.25;
  }
 
}
 
 
What is an Interface?
                An interface is defined by the key word interface. An interface has no implementation; it only has the definition of the methods without the body. When we create an interface, we are basically creating a set of methods without any implementation. A class implementing an interface must provide the implementation of the interface members. All the methods and properties defined in an interface are by default public and abstract.

Define an Interface
In the following code listing an interface named Iproduct has been defined.

Listing 2
public interface Iproduct
{
  string ID
  {
    get;
    set;
 
  }
 
  double Price
  {
    get;
    set;
 
  }
  double CalculatePrice();
}

Implementation

In the following code listing the Iproduct interface has been implemented by the class implementInterface.
Listing 3
public class implementInterface: Iproduct
{
  protected string id;
  protected double price;
  public string ID
  {
    get
    {
      return id;
    }
    set
    {
      id = value;
    }
  }
  public double price
  {
    get
    {
      return price;
    }
    set
    {
      price = value;
    }
 
  }
  public double CalculatePrice()
  {
    return Price * 1.25;

  }
 
Abstract Class vs. Interface 
·         An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
·         An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
·         An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
·         A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
·         Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
·         Abstract classes are faster than interfaces.
 

0 comments: