Working with Abstract classes, Sealed Classes, and Interfaces in C#
page 3 of 7
by SANJIT SIL
Feedback
Average Rating: 
Views (Total / Last 10 Days): 107042/ 125

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 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();
}

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;
  }

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;
  }
 
}

How to Test

In the following code listing abstract class implementation is being tested.

Listing 5

ImplementAbstract testAbstract=new implementAbstract();
testAbstract.ID=”A1”;
testAbstract.Price=1000.00;
double amt=testAbstract.CalculatePrice();
Response.Write(“Total Net Price of Product : “ +” “+testAbstract.ID +” “+ “is+” “+amt);
Response.Write(“<br>”);

In the following code listing the implementation of our interface is being tested.

Listing 6

ImplementInterface testInterface=new implementInterface();
testInterface.ID=”B1”;
testInterface.Price=900.00;
double Iamt= testInterface.CalculatePrice();
Response.Write(“Total Net Price of Product : “ +” “+testInterface.ID +” “+ “is+” “+Iamt);
Response.Write(“<br>”);

It should be noted that static members are not inherited under any circumstances. Hence we can not declare a static method in an interface because interfaces have no implementation. We can, however, declare a method as static in an abstract class, because abstract classes can have implementations of static methods. This has been described in the following code listing:

Listing 7

In an abstract class:

public static void test()
{
}

But we can not write the following in an interface.

Listing 8

static void test();

View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-06-19 7:58:35 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search