1. Abstract classes and class members
Use the abstract keyword to create an incomplete class and class member that must be implemented in a derived class.
For example:
public abstract class A
{
// Class members here.
}
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library can define an abstract class that is a parameter to its multiple functions and require the programmer to use the library to provide its own class implementation by creating a derived class.
Abstract classes can also define abstract methods. method is to add the keyword abstract to the front of the return type of the method.
For example:
public abstract class A
{
public abstract void DoWork(int i);
}
Abstract methods are not implemented, so the method definition is followed by a semicolon rather than a regular method block. Derived classes of an abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, an abstract class can override the virtual method with an abstract method.
For example:
// compile with: /target:library
public class D
{
public virtual void DoWork(int i)
{
// Original implementation.
}
}
public abstract class E : D
{
public abstract override void DoWork(int i);
}
public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}