Tuesday, August 10, 2010

Interfaces vs Abstract classes

Interfaces

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.

[edit]

Declaration

[attributes] [modifiers]

interface identifier [:base-type[,]]

{

body [;]

}

The attributes is optional and is used to hold additional declarative information.

The modifier is optional. The allowed modifiers are public and internal, unless nested inside a class, then the allowed modifiers are public, protected, private and internal. If no modifier is supplied then a default of internal is used.

The keyword interface must be followed by an identifier that names the interface.

The base-type of an interface can be zero or more interfaces. When more than one base-type is used, then this is a comma-separated list of base-types.

The body contains the member declarations.

[edit]

Implementation

An interface is defined using the keyword interface. It is common practice to start all interface names with a capital I. For example:

public interface IVehicle

{

void Start();

void Drive();

void Park();

void ChangeGear(int gear);

void SwitchOff();

}

In order to implement the interface, every method must be implemented in the class, else a compiler error will ensue.

public class Vehicle : IVehicle

{

public void Start()

{

Console.WriteLine("The vehicle has been started");

}

public void Drive()

{

Console.WriteLine("The vehicle is being driven");

}

public void Park()

{

Console.WriteLine("The vehicle is being parked");

}

public void ChangeGear(int gear)

{

Console.WriteLine("Gear changed to " + gear.ToString());

}

public void SwitchOff()

{

Console.WriteLine("The vehicle has been switched off");

}

}

If a class implements more that one interface and the two interfaces have methods named the same, then each method must be implemented unless they have the same definition in which case only one implementation is needed to implement the methods.

Here is a sample application (written in C# Express) showing the code above.

More information can be found in the 70-536 section - Implement .NET Framework interfaces - including how to get Visual Studio to fill in most of the code for you.

[edit]

Abstract classes

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.

For example, we could create an abstract class for all vehicle to inherit from:

public abstract class Vehicle

{

public void Start()

{

Console.WriteLine("The vehicle has been started");

}

public abstract void Drive();

public abstract void Park();

public abstract void ChangeGear(int gear);

public void SwitchOff()

{

Console.WriteLine("The vehicle has been switched off");

}

}

So each class that inherits from Vehicle will already be able to use the methods Start and SwitchOff, but they must implement Drive, Park and ChangeGear.

So if we were to implement a Car class, it may look something like this.

public class Car : Vehicle

{

public Car()

{

}

public override void Drive()

{

Console.WriteLine("The car is being driven");

}

public override void Park()

{

Console.WriteLine("The car is being parked");

}

public override void ChangeGear(int gear)

{

Console.WriteLine("The car changed gear changed to " + gear.ToString());

}

}

The override keyword tells the compiler that this method was defined in the base class.

[edit]

Summary

  • An Interface cannot implement methods.
  • An abstract class can implement methods.

  • An Interface can only inherit from another Interface.
  • An abstract class can inherit from a class and one or more interfaces.

  • An Interface cannot contain fields.
  • An abstract class can contain fields.

  • An Interface can contain property definitions.
  • An abstract class can implement a property.

  • An Interface cannot contain constructors or destructors.
  • An abstract class can contain constructors or destructors.

  • An Interface can be inherited from by structures.
  • An abstract class cannot be inherited from by structures.

  • An Interface can support multiple inheritance.
  • An abstract class cannot support multiple inheritance.