Saturday 14 January 2017

Abstract in c#

ABSTRACT :-The purpose of abstract class is to provide a common definition of based class that multiple derived classes can share, and can be used only as a based class and never want to create the object of this class.Any class can be convert in abstract class by adding abstract modifier to to.

Some key point of  an abstract class and method are:-

  1. An abstract class can not be instantiate.
  2.  An Abstract class can have access modifiers like public, protected, internal with class members. But abstract members cannot have private access modifier.
  3. An abstract class must have an abstract method.
  4. An abstract method is implicitly a virtual method.
  5. The class containing at least one abstract method must be declared abstract.
The purpose of an abstract class is to provide basic functionalities that multiple derived classes can share and override.
  
Example:-
       class Program
    {
        public abstract class university
        {
            public abstract void MCA();
            public abstract void MBA();
            public void Durination()
            {
                Console.WriteLine("Post Graduate fees Structure");
            }

        }
        public class WBTU:university 
        {
            public override void MCA()
            {
                Console.WriteLine("Fees is 250000");
            }
            public override void MBA()
            {
                Console.WriteLine("Fees is 5000000");
            }

        }
        public class BHU : university
        {
            public override void MCA()
            {
                Console.WriteLine("Fees is 350000");
            }
            public override void MBA()
            {
                Console.WriteLine("Fees is 450000");
            }
        }
            static void Main(string[] args)
        {
            university objUni = new WBTU();
            objUni.Durination();
            objUni.MBA();
            objUni.MCA();
            objUni = new BHU();
            objUni.MBA();
            objUni.MCA();
            Console.ReadLine();
            

        }
    }

Note:-From above example we have make one abstract class university and two abstract methods MCA and MBA. WBTU and BHU both are override university course fee.
University course common for both WBTU and BHU so university method MCA and MBA is abstract.
WBTU and BHU inherited abstract method so university method must be override here.

1 comment:

How to get logged in User's Security Roles using Java Script in dynamic CRM 365.

 function GetloggedUser () {     var roles = Xrm.Utility.getGlobalContext().userSettings.roles;      if (roles === null) return false;      ...