Interface in OOPS

Introduction

Interface is introduced to achieve multiple inheritance. Multiple classes cannot be inherited in C#, because classes have definitions for members, so the compiler will get confused with members which to invoke.

Ex:

    public class A
    {
        public int Add(double a, double b)
        {
            return System.Convert.ToInt32(a + b);
        }
    }
    public class B
    {
        public int Add(double a, double b)
        {
            int x = System.Convert.ToInt32(a);
            int y = System.Convert.ToInt32(b);
            return x + y;
        }
    }
    public class C : A, B
    {
        // not possible in C#.
    }
    public class D
    {
        public void main()
        {
            A a = new A();
            B b = new B();
            C c = new C();

            // will return 12 (5.55+6.55=12.10, So while converting .10 will be ejected bcz less than .50)
            int x = a.Add(5.55, 6.55);

            // will return 13(5.55 will be taken as 6 bcz .55 is greater than .50 also 6.55 will be taken as 7 So 6 + 7 = 13) not possible in C#
            int y = b.Add(5.55, 6.55);

            // in this case the compiler cannot take Decision on which method to invoke, because Both A and B have Add and function differently.
            int z = c.Add(5.55, 6.55);
        }
    }

To avoid such situations, interface is used. In interface members does not have definition, the must have methods and properties can be declared. So the inheriting class can define the member as required. As in class the default Access modifier for the interface is internal. But the default Access modifier of the interface member is not like in class. In interface. The default Access modifier is PUBLIC, but in class it is PRIVATE.Interface

Implicit Implementation

    interface iA // internal interface
    {
        int Add(double a, double b); // public method.
    }
    interface iB
    {
        int Add(double a, double b);
    }
    class C : iA, iB
    {
        public int Add(double a, double b)
        {
            return System.Convert.ToInt32(a + b);
        }
    }

This is implicit implementation of interfaces. Even if both interfaces have “Add” method, single method definition will satisfy both interfaces.

    class D
    {
        void main()
        {
            iA ia = new C();
            // will point to “Add” method in class C.
            ia.Add(5.55, 6.55);

            iB ib = new C();
            // will point to “Add” method in class C.
            ib.Add(5.55, 6.55);

            C c = new C();
            // will point to “Add” method in class C.
            c.Add(5.55, 6.55);
        }
    }

So there is no confusion about pointing method.

Explicit Implementation

Even if you need to implement each interface method separately, it can be done by explicit implementation. But explicitly implemented method cannot be invoked directly from derived class. Because we cannot specify any access modifiers for explicitly implemented methods. It will be private by default. It can be either accessed by.

  1. Another method in derived class
  2. Creating object for interface type.
ANOTHER METHOD IN DERIVED CLASS
    interface iA // internal interface
    {
        int Add(double a, double b); // public method.
    }

    interface iB
    {
        int Add(double a, double b);
    }
    class C : iA, iB
    {
        // Explicit implementation of “iA” interface method
        int iA.Add(double a, double b)
        {
            return System.Convert.ToInt32(a + b);
        }
        // Explicit implementation of “iB” interface method
        int iB.Add(double a, double b)
        {
            int x = System.Convert.ToInt32(a);
            int y = System.Convert.ToInt32(b);
            return x + y;
        }
        // Exposing “iA” interface method through another public method
        public int AddBeforeConverting(double a, double b)
        {
            iA ia = this;
            return ia.Add(a, b);
        }

        // Exposing “iB” interface method through another public method
        public int AddAfterConvertion(double a, double b)
        {
            iB ib = this;
            return ib.Add(a, b);
        }
    }

Here we use different method names, so no confusion.

CREATING OBJECT FOR INTERFACE TYPE
    interface iA // internal interface
    {
        int Add(double a, double b); // public method.
    }

    interface iB
    {
        int Add(double a, double b);
    }
    class C : iA, iB
    {
        // Explicit implementation of “iA” interface method
        int iA.Add(double a, double b)
        {
            return System.Convert.ToInt32(a + b);
        }
        // Explicit implementation of “iB” interface method
        int iB.Add(double a, double b)
        {
            int x = System.Convert.ToInt32(a);
            int y = System.Convert.ToInt32(b);
            return x + y;
        }
    }
    class D
    {
        void main()
        {
            iA ia = new C();
            // will return 12 as it convert after addition
            ia.Add(5.55, 6.55);

            iB ib = new C();
            // will return 13 as it convert numbers before addition
            ib.Add(5.55, 6.55);

            C c = new C();
            // Cannot use like it, method won’t be available in the object.
            c.Add(5.55, 6.55);
        }
    }

Interface

  • Interface is defined with the keyword “interface”.
  • Interfaces are used in design level.
  • Object for interface cannot be created directly. But it can be created by calling derived class constructor(as did with abstract class).
  • Interfaces only can have declaration, but not definition

Ex:

Public interface iA
{
    String Father; // field is not allowed in interface
    String Name{get;set;} // property is allowed in interface
    Int Add(int a, int b);
}
  • Member fields cannot be used in interface.
  • Access modifiers cannot be used with interface members.
  • All the interface members must be implemented in the derived classes.

After reading Abstract class and Interface, most of the people will have a confusion on why we have both abstract class and interface, and where to use them, because both have many similar features. It has specific reasons to have both together.

  • When we have a normal base class to inherit, then we cannot inherit another abstract class too.
    class A
    {
        private string _name;
        public string GetName()
        {
            return _name;
        }
    }
    abstract class B
    {
        public abstract void SetName(string Name);
    }

    interface iC
    {
        void SetName(string Name);
    }
    class D : A, B
    {
        // This is not possible, because multiple inheritance on class is Not possible in C#
            }
    class D : A, iC
    {
        //This is possible. Here only one class is inherited and another one is interface
    }

In the above situation we need to use interface instead of abstract class.

  • When we are in need of some method declarations and some methods with method definition, then we have only one choice of Abstract class.(some function in a fixed manner and some function according to the consumer requirement on implementation).

There may be more reasons, so please refer the internet also. As said above, the abstract class and interface are used in the design level. So it is mostly used in design patterns.(if you wish to learn more about design patterns, can refer to internet) For more references about OOPS: MSDN – OOPS(C#)

Refer for more OOPS Concepts

Leave a Reply

%d bloggers like this: