Working with Interfaces

  • If a class contain all abstract functions then it is known as an Interface
  • To create an Interface use interface keyword
  • An interface can contain
          1)Abstract function
          2)Properties
          3)Indexes
          4)Events
  • An Interface cannot contain
         1.Non-Abstract Functions
         2.Data Fields
         3.Constructors
         4.Destructors
  • By default Interface functions are treated as Public and abstract
  • An Interface can’t be instantiated directly
  • Creating / deriving a new class or other Interface is compulsory / mandatory in order to provide implementation to its abstract function
  • An interface can itself inherit from multiple interfaces
  • An Interface is used to Implementation Multiple Inheritance in C#.NET
  • In general an Interface provides control over the classes
  • An Interface represents a contract, in that a class that implements an interface must implement every aspect of that interface exactly as it is defined
  • Interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes
  • We can define features as small groups of closely related members and can develop enhanced implementations for your interfaces without jeopardizing existing code, thus minimizing compatibility problems
  • It is also possible to add new features at any time by developing additional interfaces and implementations
  • Although interface implementations can evolve, interfaces themselves cannot be changed once published
  • Changes to a published interface may break existing code
  • If you think of an interface as a contract, it is clear that both sides of the contract have a role to play
  • The publisher of an interface agrees never to change that interface, and the implementer agrees to implement the interface exactly as it was designed

      Example:-

      Class Diagram

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

namespace CAInterface
{
    interface ClsEmployee
    {
        void GetEmpData();
        void DisplayEmpData();
    }
    class ClsManager:ClsEmployee
    {
        int EmpId;
        string EName;
        double Bonus;
        double Ca;
        public void GetEmpData()
        {
            Console.WriteLine("Enter the Manager Id,Name,Bonus,Ca");
            EmpId = Convert.ToInt32(Console.ReadLine());
            EName = Console.ReadLine();
            Bonus = Convert.ToDouble(Console.ReadLine());
            Ca = Convert.ToDouble(Console.ReadLine());
        }
        public void DisplayEmpData()
        {
            Console.WriteLine("Manager  Details Are:-");
            Console.WriteLine("Manager Id is:-     "+EmpId);
            Console.WriteLine("Manager Name is:-   "+EName);
            Console.WriteLine("Manager Bonus is:-  "+Bonus);
            Console.WriteLine("Manager Ca is:-     "+Ca);
       
        }
    }
    class ClsInterface
    {
        static void Main(string[] args)
        {
            ClsManager obj1 = new ClsManager();
            obj1.GetEmpData();
            obj1.DisplayEmpData();
            Console.ReadLine();
        }
    }
}



Output :- 




No comments:

Post a Comment