Saturday, 10 March 2012

Interface implementation implicit and Explict implementations?


Yes,we know Interface members are implemented in multiple classes to maintain good standardization.what are the common method belogs various class define in one place that is Interface.
Interface are pure abstract class we can say,Inface is light weight type in entire dotnet.Interface are avoiding decopuling mechanism. Any no’f classes can implement these interface members.
Note :Interface we can’t create instance for interfaces.[why because interface defined not for creating instance purpose]
 Interfaces supports Multiple inhertience*******

Ex: IEnumerable,IEnumerator,IFormatable…..ect

What is Multiple inhertience?

 A Class is derived from multiple classes is called multiple inhertience. we have ambiguity problem with class which the methods with the same name.so complier doesn’t when creating object and calling methods which raises an exception.

Like
Class First
Public string Show()    // ambugity problem
{
return  “this is First implementation”;
}
Class Second
{
Public  string  Show()   // ambugity problem
{
                return  “this is second implementation”;
}
Both classes have same method name is called Show() method in derived class when we class this method complier doesn’t which method it has to call form First class Show() or  Second class Show() method. Complier error say it is ambuigty problem.
So dotnet isn’t  supporting this Multiple inhertience with the help of interfaces. Even we want to write also like
Class Class3: Class1,…..here no support for intelligence also.
How interfaces supports this interface Multiple inhertience implementation?
Yes, Interfaces uses Explict interface implementation to support this Multiple inhertice inhertience.
Ex:
interface IFirst
    {
        string Msg();
    }
    interface ISecond
    {
        string Msg();
    }
We have 2 interfaces with same method name is Msg() when we implementation these we have to use interface implementation Explict option from intelligence.

class Message:IFirst,ISecond
    {

        static void Main()
        {
             Message msgobj = new Message();
            string str = msgobj.Msg();
            Console.Write(str); // IFrist implict 
            IFirst Irefirst = (IFirst)msgobj;
            string s = Irefirst.Msg();
            Console.Write(s);  //  IFirst Explict
            ISecond Irefvar = (ISecond)msgobj;
            string strr=Irefvar.Msg();
            Console.Write(strr);  // ISecond Explict         
            Console.Read();
        }
        #region IFirst Members
        public string Msg()
        {
            return "this is Ifirst implementation";
        }
        #endregion
        #region ISecond Members
        string ISecond.Msg()
        {
            return "this is ISecond implementation";
        }

        #endregion
        #region IFirst Members
        string IFirst.Msg()
        {
            return "this is Ifirst Explict implementation";
        }
        #endregion
    }
When we implement the first interface then intelligence only say we have a method the same name so use explict interface implementation.
Here I uses IFirst interface with implict(normal) implementation and expliect interface implementation.

No comments:

Post a Comment