Tuesday, April 21, 2015

Abstract class in C#

Abstract class is one of the most discussed things in C#. But for beginner they always found difficulties to understand it. They quite have a good reasons for that. The main question which every one tries to crumbling is why do we need an abstract class. We have interface to specifies signature. Well' if you are thinking in this direction you are right. Before going deep down to abstract class lets do some basic.

As per MSDN "The abstract keyword enables us to create classes and class members that are incomplete and must be implemented in a derived class". The golden rules of abstract class are 


1. The keyword abstract is used to specify class, method, property, indexer, and event as abstract.

2. If a class is specify as abstract it must be public or internal.

3. If a method, property, indexer, and event is declared as abstract then the class should also be declared as abstract.

4. We cannot create an object of a abstract class. Abstract class can only be inherited.


Example


    public abstract class BasicPay
    {
        public int FixConponent=10000;
        public abstract double GetBasic(int Basic);

        public double GetSalary(int Basic)
        {
            return GetBasic(Basic) + FixConponent;
        }
    }


In this example the class BasicPay is declared as abstract. It has two methods. GetBasic and get GetSalary. Here GetSalary has its body and declaration, because the business logic of behind this method will be same across all sub classes. But GetBasic method is declared as abstract because it will be different on different sub classes. Abstract method can be override in sub classes using override keyword. The below code snippet show how inherit and abstract class.



    public class SalesSalary : BasicPay
    {
        public override double GetBasic(int Basic)
        {
            //do some logic.
            return Basic * 5;
        }
    }
    public class ITSalary : BasicPay
    {
        public override double GetBasic(int Basic)
        {
            //do some logic.
            return Basic * 7;
        }
    }


This example clarify the objective of an abstract class. This is the one and only reason that we need and abstract class over interface. In short we need an abstract class where signatures are different for business logic and also some implementations required for consistency through out the sub classes. Here GetBasic is a signature and has different meaning for different sub classes and GetSalary is consistent across all sub classes.


Complete program


using System;

namespace Tutorials_AbstractClass
{
    //abstract class
    public abstract class BasicPay
    {
        public int FixConponent=10000;
        public abstract double GetBasic(int Basic);

        public double GetSalary(int Basic)
        {
            return GetBasic(Basic) + FixConponent;
        }
    }

    public class SalesSalary : BasicPay
    {
        public override double GetBasic(int Basic)
        {
            //do some logic.
            return Basic * 5;
        }
    }
    public class ITSalary : BasicPay
    {
        public override double GetBasic(int Basic)
        {
            //do some logic.
            return Basic * 7;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // creating object of derived class
            SalesSalary ss= new SalesSalary();
            ITSalary it = new ITSalary();

            Console.Clear();
            Console.WriteLine("Enter basic salary :");
            int BasicSalary = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Salary for IT : {0}",it.GetSalary(BasicSalary));
            Console.WriteLine("Salary for Sales : {0}", ss.GetSalary(BasicSalary));
            Console.ReadLine();
        }
    }
}

Output

Enter basic salary :       
3000                           
Salary for IT : 31000    
Salary for Sales : 25000


Happy Coding...)