Abstract classes have the following features:
1. An abstract class cannot be instantiated.
2. An abstract class may contain abstract methods and accessors.
3. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
4. It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
Abstract Methods have the following features:
1. An abstract method is implicitly a virtual method.
2. Abstract method declarations are only permitted in abstract classes.
3. Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ }) following the signature.
Example:
public abstract void ExampleMethod();
4. The implementation is provided by an overriding method, which is a member of a non-abstract class.
5. It is an error to use the static or virtual modifiers in an abstract method declaration.
Abstract Properties:
Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.
1. It is an error to use the abstract modifier on a static property.
2. An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
using System;
abstract class MyBaseC // Abstract class
{
protected int x = 100;
protected int y = 150;
public abstract void MyMethod(); // Abstract method
public abstract int GetX // Abstract property
{
get;
}
public abstract int GetY // Abstract property
{
get;
}
}
class MyDerivedC: MyBaseC
{
public override void MyMethod()
{
x++;
y++;
}
public override int GetX // overriding property
{
get
{
return x+10;
}
}
public override int GetY // overriding property
{
get
{
return y+10;
}
}
public static void Main()
{
MyDerivedC mC = new MyDerivedC();
mC.MyMethod();
Console.WriteLine("x = {0}, y = {1}", mC.GetX, mC.GetY);
}
}