Tuesday 24 June 2014

When to use abstract class?

What is abstract class?

Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes. Abstract class members marked as abstract must be implemented by derived classes.

When to use abstract class?

The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.


How toCreate use abstract class?

    public abstract class AbsAcType
    {
        public int ACno { get; set; }
        public String ACFirstName { get; set; }
        public String ACLastName { get; set; }
        public decimal Amount { get; set; }
        public decimal InterestRate { get; set; }

        public string CustomerName()
        {
            return ACFirstName + " " + ACLastName;
        }

        public string AcountNo(string BankName)
        {
            return BankName + ACno;
        }

        public abstract decimal AcBalance();

    }



    public class SavingAC : AbsAcType
    {
        public override decimal AcBalance()
        {
            return Amount + InterestRate*(decimal)0.20;
        }
    }

    public class currentAC : AbsAcType
    {
        public override decimal AcBalance()
        {
            return Amount + InterestRate * (decimal)0.028;
        }

    }



        protected void btnSavingAC_Click(object sender, EventArgs e)
        {
            decimal amount,interestRate;
            decimal.TryParse(txtAmount.Text,out amount);
            decimal.TryParse(txtInterestRate.Text,out interestRate);
            string firstName = txtFirstName.Text, lastName = txtLastName.Text;
            AbsAcType objSaving = new SavingAC()
            {
                ACno= 101,
                ACFirstName = firstName,
                ACLastName = lastName,
                Amount=amount,
                InterestRate=interestRate               
            };
            lblAcNo.Text = objSaving.AcountNo("CITI");
            lblFullname.Text= objSaving.CustomerName();
            lblResBal.Text =  objSaving.AcBalance().ToString();
         }

        protected void btncurrentAC_Click(object sender, EventArgs e)
        {
            decimal amount, interestRate;
            decimal.TryParse(txtAmount.Text, out amount);
            decimal.TryParse(txtInterestRate.Text, out interestRate);
            string firstName = txtFirstName.Text, lastName = txtLastName.Text;
            AbsAcType objcurrent = new currentAC()
            {
                ACno = 101,
                ACFirstName = firstName,
                ACLastName = lastName,
                Amount = amount,
                InterestRate = interestRate
            };
            lblAcNo.Text = objcurrent.AcountNo("CITI");
            lblFullname.Text = objcurrent.CustomerName();
            lblResBal.Text = objcurrent.AcBalance().ToString();
        }