Abstract Classes

C# allows both classes and functions to be declared as abstract. An abstract class cannot be instantiated, while an abstract function does not have an implementation, and must be overridden in any non abstract derived class.
Obviously, an abstract function is automatically virtual. If any class contains any abstract  functions, then that class is also abstract and must be declared as such. 
  • Abstract class declaration is:
abstract class MyBaseClass
{
       
}







  • An example of an abstract method:
abstract class Value
{
   public abstract decimal CalculateCost(); // abstract method

}


Example:

    abstract class MyBaseClass
    {
        public abstract int Value_I(int n, int m);
        public abstract int Value_II(int n, int m);
    }

    abstract class MyDerivedClass : MyBaseClass
    {
        public override int Sum(int n, int m)
        {
            return n + m;
        }
    }
    abstract class MyDerivedClass1 : MyDerivedClass
    {
        public override int Prod(int n, int m)
        {
            return n * m;
        }
    }

No comments:

Post a Comment