C Sharp Programming/Abstract classes
From Wikibooks, the open-content textbooks collection
In general terms, an interface is the set of public members of a component. Of course, a C# interface is an interface that defines a set of public members. A C# class also defines an interface because it has a set of public members. A nonabstract C# class also defines the implementation of each member.
In C# it is possible to have a type that is intermediate between a pure interface that does not define any implementation, and a type that defines a complete implementation. This is called an abstract class.
You define an abstract class by including the abstract keyword on the class definition.
An abstract class is somewhere between a C# interface and a nonabstract class. Of the public members defined by an abstract class, any number of those members may include an implementation.
For example, an abstract class might provide an implementation for none of its members.
public abstract class AbstractShape
{
public abstract void Draw(Graphics g);
public abstract double X {get; set;}
public abstract double Y {get; set;}
}
This class is equivalent to an interface in many respects. (One difference is that a class that derives from this class cannot derive from any other class.)
An abstract class may also define all of its members.
public abstract class AbstractShape
{
private double x;
private double y;
//
// ... (other members)
//
public void Draw(Graphics g) {g.DrawRectangle(Pens.Black, g_rect);}
public double X {get{return x;}}
public double Y {get{return y;}}
}
And an abstract class may define some of its members but leave others undefined.
public abstract class AbstractShape
{
private double x;
private double y;
//
// ... (other members)
//
public abstract void Draw(Graphics g);
public double X {get{return x;}}
public double Y {get{return y;}}
}
An abstract class is similar to a nonabstract class, but there are some important differences.
For one thing, you cannot create an instance of an abstract class with the new keyword. For example, the following statement will raise a compiler error:
AbstractShape shape = new AbstractShape();
Of course, assuming the concrete class Square derives from AbstractShape, the following would be correct:
AbstractShape shape = new Square();
A second difference is that an abstract class can contain abstract members. As was shown above, it does not have to contain abstract members. The point is that a nonabstract class may not contain abstract members. That is, you must include the abstract keyword on the class if you include even one abstract member.
The third difference is that an abstract class cannot be sealed. That is, you cannot use both the abstract keyword and the sealed keyword on the same class.
[edit] Implementing methods
As with virtual methods, you can implement abstract methods or properties with the override keyword:
public class Rectangle : AbstractShape { private double x; private double y; // ... public override void Draw(Graphics g) { g.DrawRectangle(Pens.Black, g_rect); } public override double X { get { return x; } set { x = value; } } public override double Y { get { return x; } set { x = value; } } }
Overriding an abstract method is effectively the same as overriding a virtual method - you cannot change the access specifiers (i.e. you can't convert a protected abstract method into public), and you cannot add a missing get or set to an abstract property.