What is Abstract Class in C#.Net,How to use Abstract Class in c#.net


•    Abstract class is one type of half define parent class.
•    Its complete implementation is in child class.
•    You cannot create object of abstract class.
•    You can use this abstract class by make its child class and call parent class (Abstract class) method using child class object ass show in Ex.

Example:

namespace CA_AbstactClass
{
    // define abstract class
    abstract class operation
    {
        // define abstract method
        //you can only define but on not implement the method in this class
        public abstract int addition(int n1,int n2);

        // define non abstract method
        public int multiplication(int n1, int n2)
        {
            return n1 * n2;
        }
       
    }
    class Program:operation
    {
        static void Main(string[] args)
        {
            // you can create object of 'Program' class but not 'operation' class
            Program objProgram = new Program();
     int add =objProgram.addition(10,20);
            int mul=objProgram.multiplication(10,20);
            Console.WriteLine("Added : {0}, Multiplied : {1}", add, mul);

        }
       
        // now override the addition method in this class
        // access modifier of abstract method must be same in parent class and child class
        public override int addition(int n1, int n2)
        {
            return n1 + n2;
        }
    }
}

  • If you  have use multiple abstract class they inherit each other than you can implement  abstract method of parent abstract class in child abstract class.
Example:
namespace CA_AbstactClass
{
    // define abstract class
    abstract class operation
    {
        // define abstract method
        //you can only define but on not implement the method in this class
        public abstract int addition(int n1, int n2);

        // define other abstract method
        public abstract int multiplication(int n1, int n2);

    }
   
    abstract class operation1:operation
    {
        // Implementing abstract method
        public override int multiplication(int n1, int n2)
        {
            return n1*n2;
        }
       
    }
    class Program : operation1
    {
        static void Main(string[] args)
        {
            // you can create object of 'Program' class but not 'operation' class
            Program objProgram = new Program();
            int add =objProgram.addition(10,20);
            int mul=objProgram.multiplication(10,20);
            Console.WriteLine("Added : {0}, Multiplied : {1}", add, mul);
        }

        // now override the addition method in this class
        // access modifier of abstract method must be same in parent class and child  class
        public override int addition(int n1, int n2)
        {
            return n1 + n2;
        }
    }
}


    Rules For Abstract class
•    Abstract class can have abstract method or non-abstract method also
•    Abstract method cannot be private.
•    Abstract class cannot be sealed.
•    Abstract method cannot be virtual
•    Abstract member can note be static.
•    Abstract method must be define in only abstract class.


See Also :
  Follow Me On Facebook

--
/\/ir@\/  <(.'.)>

Comments

Popular posts from this blog

How to make and use web service in Microsoft Dynamics Navision.

How to use Format function in Microsoft Dynamics Navision.

How to create simple report in Microsoft Dynamics Navision.