Abstract class is used to create a base class that can be shared by multiple derived classes.
- It cannot be instantiated.
- It may define abstract methods, which have no implementation.
- All the abstract methods must be implemented by its derived classes.
- It is created by using the abstract keyword.
- It can contain both abstract and non-abstract methods.
Example - Abstract class with abstract method
public abstract class MyAbstractClass
{
public abstract void MyAbstractMethod(int number);
}
Abstract class can change the original implementation of a base class it inherits.
Example
public class MyBaseClass
{
public virtual void MyMethod(int number)
{
// Original implementation.
}
}
public abstract class MyAbstractClass : MyBaseClass
{
public abstract override void MyMethod(int number);
}
public class MyDerivedClass : MyAbstractClass
{
public override void MyMethod(int number)
{
// New implementation code.
}
}