Sunday, August 16, 2015

Can you inherit multiple interfaces in C#?

Implement Multiple Interfaces

Yes, we can implement multiple interfaces in one class. This is only the way to achieve multiple inheritances in C#.

Example:

       public interface MyInterface1
    {
        int Add(int i, int j);
    }

    public interface MyInterface2
    {
        int Sub(int i, int j);
    }

    public class Math : MyInterface1, MyInterface2
    {
        public int Add(int i, int j)
        {
            return i + j;
        }

        public int Sub(int i, int j)
        {
            return i - j;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Math obj = new Math();

            int i = obj.Add(20, 10);
            Console.WriteLine(i);

            int j = obj.Sub(20, 10);
            Console.WriteLine(j);

        }

    }

No comments:

Post a Comment