June 20, 2011

Code Example for Interface and Abstract Class in C#


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


namespace ConsoleApplication2
{




    interface EMPLOYEE
    {
         int GetID
        {
            get;


            set;
        }


         string GetName
         {
             get;


             set;
         }




         string  GetDepName
         {
             get;
         }


         int GetDepID
         {
             get;
         }
    }




    public  abstract class EMP:EMPLOYEE 
    {
        int ID;
        String  NAME;


        public EMP()
        {


        }
       public EMP(int id,String name)
        {
            this.ID =id;
            this.NAME = name;


        }
        public  int GetID
        {
            get{


                return this.ID;
            }


            set{


                this.ID=value ;
            }
        }


        public string  GetName
        {
            get
            {


                return this.NAME ;
            }
            set
            {


                this.NAME = value;
            }
        }




           public abstract string  GetDepName
           {
            get;
           }


         public  abstract  int GetDepID
        {
            get;
        }
       
    }


    public class SalesEmp : EMP
    {
        int DepartmentID;
        String DepartmentName;


        public SalesEmp()
        {


        }
        public SalesEmp(int DepId,String DepName)
        {
            this.DepartmentID = DepId;
            this.DepartmentName = DepName;


        }
      
        public override  int GetDepID
        {
            get
            {


                return this.DepartmentID;
            }
        }


        public override  string GetDepName
        {
            get
            {


                return this.DepartmentName;
            }
        }


    }




    public class TechEmp : EMP
    {
        int DepartmentID;
        String DepartmentName;


        public TechEmp()
        {


        }
        public TechEmp(int DepId, String DepName)
        {
            this.DepartmentID = DepId;
            this.DepartmentName = DepName;


        }


        public override int GetDepID
        {
            get
            {


                return this.DepartmentID;
            }
        }


        public override string GetDepName
        {
            get
            {


                return this.DepartmentName;
            }
        }


    }


    
    class Program
    {
        static void Main(string[] args)
        {
            EMPLOYEE se = new SalesEmp(1, "SALE");
            EMPLOYEE te = new TechEmp (2,"TECHNICAL");


            se.GetName = "EMP1";
            se.GetID = 1;


            te.GetName = "EMP2";
            te.GetID = 2;


            Program obj = new Program();
            obj.EmployeeBelongTo(se);
            obj.EmployeeBelongTo(te);
        }


        public void EmployeeBelongTo(EMPLOYEE E)
        {


            Console .WriteLine ("{0} belongs to {1} department",E.GetName,E.GetDepName);
        }
    }






}

No comments:

Post a Comment