Thursday, April 18, 2013

what is interface, understanding on interface in c#, how to implement interface in c#,Real Time example


Interface:
1. Interface is a way to provide communication between user and program classes, methods,structures.
2. Interfaces in C # provide a way to achieve runtime polymorphism.
3. can not create instance of an interface
4. Using interfaces we can invoke methods from different classes through the same Interface reference.
5. using virtual method we can invoke mthods from different classes in the same inheritance hierarchy through the same reference. 
6. An abstract class serves as the base class for a family of derived classes, while interfaces are meant to be mixed in with other inheritance trees
7. Interface does not contain any concrete methods.
8. Multiple Inheritance is possible with interface
9. Access Specifiers are not supported in Interface(Interface is a contract with the outside world which specifies that class implementing this interface does a certain set of things. So, hiding some part of it doesn't make sense.However, interfaces themselves can have access specifiers like protected or internal etc. Thus limiting 'the outside world' to a subset of 'the whole outside world'.)
10. All the interface methods are Public. You can't create an access modifier in interface.

Sample Example:
namespace InterfaceDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ImplementedMainClass obj = new ImplementedMainClass();

            //I want to call first interface methods
            ISampleInterface sample = obj;
            sample.getData();
            sample.saveData();
            sample.checkStatus();

            //I want to call second interface methods
            int id=2;
            ISampleInterface2 sample2 = obj;
            sample2.getDataByID(id);
            sample2.createUserCredentials("demo", "demopwd");
        }
    }


    public interface ISampleInterface
    {
        DataTable getData();
        string saveData();
        string checkStatus();
    }
    public interface ISampleInterface2           
    {
        DataTable getDataByID(int id);
        string createUserCredentials(string userName,string password);
    }

    public class ImplementedMainClass : ISampleInterface, ISampleInterface2
    {
              

        #region SampleInterface2 Members

        public DataTable getDataByID(int id)
        {
            DataTable dt = new DataTable();
            ////Write code for retrive data by using id from database\
            return dt;
        }

        public string createUserCredentials(string userName, string password)
        {
           //Write a logic for save user credentials into database
            return string.Empty;
        }

        #endregion

        #region SampleInterface Members

        public DataTable getData()
        {
            DataTable dt = new DataTable();
            //Write code for retrive data from database
            return dt;
        }

        public string saveData()
        {
            //Write code for insert/update/delete 
            return string.Empty;
        }

        public string checkStatus()
        {
            return string.Empty;
        }

        #endregion
    }
    
}


No comments:

Post a Comment