Interface Concept in Programming

Concept
Consider the you are the employee of IT company
Company provide you one standard card 
Property of card
1. Shape
2. Placement of words
3. Photo 
4. Info
Above properties are arranged as below format




Just imagine - If employee use different id card shapes and size. it will mess 
to over come this issue and implement standard for all employee we should create interface and implement when we create any employee id card

Key Points 
This is common interface for each and every employee of the company
Can we restrict to use of ID card interface?
No, it will be public by default.
Can we set any name or info directly into the ID card?
No, it will not allow to set into directly, it will just provide frame or structure
you can implement using it. if you try to implement any info, it violate interface property(when interface change all card will get impacted). 

Execution 
We have created the ICard interface
Now we can make any employee card.

interface ICard
{
        string CompanyName { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string EmployeeID { get; set; }

        string DisplayEmployeeName();
}

//Create ID card for Parmanet employee
class ParmanentEmployeeCard : ICard
{
        public string CompanyName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmployeeID { get; set; }
        public string DisplayEmployeeName()
        {  
            return $"{FirstName} {LastName}";
        }
}
class Program
{
        static void Main(string[] args)
        {
            ICard card = new ParmanentEmployeeCard();
            card.CompanyName = "XYZ Technology";
            card.EmployeeID = "2R451E1542";
            card.FirstName = "Sam";
            card.LastName = "adam";

            Console.WriteLine("Parmanent Employee");
            Console.WriteLine(card.CompanyName);
            Console.WriteLine(card.EmployeeID);                            Console.WriteLine(
                          card.DisplayEmployeeName());
            Console.ReadLine();
        }
    }

Output
Permanent Employee
XYZ Technology
2R451E1542
Sam adam

How we can handle other employee ICard like contract?
This interface will handle it like below

interface ICard
{
        string CompanyName { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string EmployeeID { get; set; }

        string DisplayEmployeeName();
}

//Create ID card for Parmanet employee
class ParmanentEmployeeCard : ICard
{
        public string CompanyName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmployeeID { get; set; }

        public string DisplayEmployeeName()
        {  
            return $"{FirstName} {LastName}";
        }

//Create ID card for Parmanet employee
class ContractEmployee : ICard
{  
        public string CompanyName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmployeeID { get; set; }

        public string DisplayEmployeeName()
        {
            return $"{FirstName} {LastName}";
        }
}
class Program
{
        static void Main(string[] args)
        {
            ICard card = new ParmanentEmployeeCard();
            card.CompanyName = "XYZ Technology";
            card.EmployeeID = "2R451E1542";
            card.FirstName = "Sam";
            card.LastName = "adam";

            Console.WriteLine("Parmanent Employee");
            Console.WriteLine(card.CompanyName);
            Console.WriteLine(card.EmployeeID);
            Console.WriteLine(
            card.DisplayEmployeeName());


            card = new ContractEmployee();
            card.CompanyName = "XYS Company";
            card.EmployeeID = "2245111654542";
            card.FirstName = "Jonh";
            card.LastName = "Mark";

            Console.WriteLine("\nContract Employee");
            Console.WriteLine(card.CompanyName);
            Console.WriteLine(card.EmployeeID);


            Console.WriteLine(
            card.DisplayEmployeeName()+"\n");
            
            Console.ReadLine();
        }
}

Output
Permanent Employee
XYZ Technology
2R451E1542
Sam adam

Contract Employee
XYS Company
2245111654542
Jonh Mark



Multiple Interfaces

    interface ICard
    {  
        string CompanyName { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string EmployeeID { get; set; }       
        string DisplayEmployeeName();
    }

    interface ICardTemp
    {
        string VisitorName { get; set; }
        string ContactPerson { get; set; }
    }

//Create ID card for Permanent employee
 class PermanentEmployeeCard : ICard
  {
        public string DisplayEmployeeName()
        {
            return $"{FirstName} {LastName}";
        }
        public string CompanyName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmployeeID { get; set; }
}

class ContractEmployee : ICard, ICardTemp
{
        public string CompanyName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmployeeID { get; set; }
        public string VisitorName { get; set; }
        public string ContactPerson { get; set; }

        public void CreateTempCard()
        {
            Console.WriteLine($"Temp Card \n {VisitorName} \n {ContactPerson}");
        }
        public string DisplayEmployeeName()
        {
            return $"{FirstName} {LastName}";
        }
}

class Program
{
        static void Main(string[] args)
        {
            ICard card = new ParmanentEmployeeCard();
            card.CompanyName = "XYZ Technology";
            card.EmployeeID = "224511542";
            card.FirstName = "Sam";
            card.LastName = "adam";

            Console.WriteLine("Permanent Employee");
            Console.WriteLine(card.CompanyName);
            Console.WriteLine(card.EmployeeID);
            Console.WriteLine(card.DisplayEmployeeName());

            card = new ContractEmployee();
            card.CompanyName = "XYS Company";
            card.EmployeeID = "2245111654542";
            card.FirstName = "Jonh";
            card.LastName = "Mark";

            Console.WriteLine("\nContract Employee");
            Console.WriteLine(card.CompanyName);
            Console.WriteLine(card.EmployeeID);
            Console.WriteLine(card.DisplayEmployeeName()+"\n");

            ContractEmployee Tempcard = new ContractEmployee();
            Tempcard.VisitorName = "Robert";
            Tempcard.ContactPerson = "John Mark";
            Tempcard.CreateTempCard();

            Console.ReadLine();
        }
}

Output
Permanent Employee
XYZ Technology
224511542
Sam adam

Contract Employee
XYS Company
2245111654542
Jonh Mark

Temp Card
 Robert
 John Mark

Errors In Interface


1.Cannot contain fields

interface ICard
{
        string companyName;
}

Output
Error        CS0525        Interfaces cannot contain fields

2. Interface members cannot have a definition


interface ICard
{
        string CompanyName { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string EmployeeID { get; set; }
        void DisplayName()
        {
            Console.WriteLine($"{FirstName} {LastName}");
        }
        string DisplayEmployeeName();
}

Output
Error        CS0531        'ICard.DisplayName()': interface members cannot have a definition

3.does not implement interface member


interface ICard
{
        string CompanyName { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string EmployeeID { get; set; }

        string DisplayEmployeeName();
 }

//Create ID card for Parmanet employee
class ParmanentEmployeeCard : ICard
{
        public string CompanyName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmployeeID { get; set; }

       //If you miss any of the method from interface, compile error
       //public string DisplayEmployeeName()
       // {  
       // return $"{FirstName} {LastName}";
        //}
}

Output
Error        CS0535        'ParmanentEmployeeCard' does not implement interface member 'ICard.DisplayEmployeeName()'

**** Sequence doesn't matter for implementation

4.cannot implement an interface member because it is not public



interface ICard
{
        string CompanyName { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string EmployeeID { get; set; }

        string DisplayEmployeeName();
}

class ParmanentEmployeeCard : ICard
{
      string DisplayEmployeeName()
        {
            return $"{FirstName} {LastName}";
        }
        public string CompanyName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmployeeID { get; set; }
}

Output
Error        CS0737        'ParmanentEmployeeCard' does not implement interface member 'ICard.DisplayEmployeeName()'.
'ParmanentEmployeeCard.DisplayEmployeeName()' cannot   
implement an interface member because it is not public.        


No comments

sblog4u. Powered by Blogger.

Topics

Real World Programming Concept in 2 mins

Clone

What is Clone ? It look like duplicate of original Reference is same for string And different for objects Once clone crea...