In this blog I am trying to explore the concept of Abstract class in c#:

An Abstract class is an incomplete class (because of partial implementation) or special class.  Abstract class contains at least one abstract method and any number of defined method, we can't instantiate to abstract class because abstract method (suppose if you able to create instance of abstract class ,then you may be call abstract method of abstract class which is invalid method call(method are not defined)) .  We can use an Abstract class as a Base Class. An Abstract method must be implemented in the non-Abstract class using the override keyword. After overriding the abstract method is in the non-Abstract class. We can derive this class in another class and again we can override the same abstract method with it. 

Abstract classes have the following features:

  • An abstract class cannot be instantiated.
  • An abstract class may contain abstract methods and accessors.
  • It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace abstract_class
    class Program
        static void Main(string[] args)
            mathOperation mo = new mathOperation();
            int res;
            res = mo.add(5, 200);
            res = mo.sub(50, 20);//base class method after override
            Console.WriteLine("sum ={0}", res);
            res = mo.divide(10,2);
            Console.WriteLine("result of overloded function:{0}", res);
    class mathOperation : operation
    {// base class abstract method implimented here
        public override int add(int n1, int n2)
            Console.WriteLine("the sum of number:{0}+{1}={2}", n1, n2, n1 + n2);             int res = n1 + n2;
            return (res);
        //override base class method
        public override void message()
            Console.WriteLine("good evening ");
        public override int sub(int n1, int n2)
            Console.WriteLine("the subtraction of number:{0}-{1}", n1, n2);
            return (n1 - n2);
        public override int multi(int n1, int n2)
            Console.WriteLine("the multiplication of number:{0}*{1}", n1, n2);             return (n1 * n2);
        public override int divide(int n1, int n2)
            Console.WriteLine("the divide of number:{0}/{1}", n1, n2);
            return (n1 / n2);
     abstract class operation
        public int number1, number2;
        public operation(int n1 = 0, int n2 = 0)
            this.number1 = n1;
            this.number2 = n2;
         public abstract int add(int n1, int n2);
         public abstract int sub(int n1, int n2);
         public abstract int multi(int n1, int n2);
         public abstract int divide(int n1, int n2);
         public virtual void message()
        {             Console.WriteLine("this is simple method of abstract class ");

  Modified On Sep-18-2014 01:24:15 PM

Leave Comment