Home / C# / Abstract Class and Interfaces in csharp / Advantages and Disadvantages of Interfaces In C#

Advantages and Disadvantages of Interfaces In C#

Disadvantage of using Interface
 
One drawback of using interfaces is that it makes the code more complicated to understand.
 
Advantage of using Interface
 
The major advantage of using interfaces is that you add an extra dimension of communication to your design. Instead of only accessing classes through class inheritance, with interfaces you can make a backdoor, open for classes implementing a specific interface.
 
This construct makes the software flexible for future changes. The client using your class through a method argument or instance variable will not have to know anything about the class, but that it implements a specific interface.
 
Meaning: all classes implementing a given interface can be used. Uses of the interface construct decouple your design, resulting in low coupling and high cohesion. Low coupling means that when classes are decoupled they do not depend closely on each other. The chance of failures, faults or errors getting thrown when you make changes to existing code will be lower when the classes are decoupled. Often you do not have to change existing code.
 
All you do is add new code. Design of interfaces and their use in design patterns is at a more abstract level compared to the abstract level where your code is at. In fact, design patterns and the use of interface, abstracts the code itself. It means that if you have your design ready you can make the source code either in Java or C# directly from the design.
 
Practical Example (Abstract Class):
 
testAbstraction.cs
 
using System;
using System.Collections.Generic;
using System.Text;

namespace testAbstraction
{
    //Creating an Abstract Class
    abstract class abstractClass
    {
        //A Non abstract method
        public int AddTwoNumbers(int Num1, int Num2)
        {
            return Num1 + Num2;
        }

        //An abstract method, to be
        //overridden in derived class
        public abstract int MultiplyTwoNumbers(int Num1, int Num2);
    }

    class Program:abstractClass
    {
        static void Main(string[] args)
        {               
            //You can create an
            //instance of the derived class
            string a,b;
            Program calculate = new Program();
            Console.Write("Enter first number:  ");
            a = Console.ReadLine();
            Console.Write("Enter second number: ");
            b = Console.ReadLine();
            int added = calculate.AddTwoNumbers(int.Parse(a),int.Parse(b));
            int multiplied = calculate.MultiplyTwoNumbers(int.Parse(a),int.Parse(b));
            Console.WriteLine("\nAdded : {0}\nMultiplied : {1}", added, multiplied);
            Console.Read();
        }
        //using override keyword,
        //implementing the abstract method
        //MultiplyTwoNumbers
        public override int MultiplyTwoNumbers(int Num1, int Num2)
        {
            return Num1 * Num2;
        }
    }
}
 
The Output is:
 
img
 
Practical Example (Interface):
 
testInterface.cs
 
using System;
using System.Collections.Generic;
using System.Text;

namespace testInterface
{
    interface IParentInterface
    {
        void ParentInterfaceMethod();
    }

    interface IMyInterface : IParentInterface
    {
        void MethodToImplement();
    }

    class Program : IMyInterface
    {
        static void Main(string[] args)
        {
            Program iImp = new Program();
            iImp.MethodToImplement();
            Console.ReadLine();
            iImp.ParentInterfaceMethod();
            Console.ReadLine();
        }

        public void MethodToImplement()
        {
            Console.WriteLine("MethodToImplement() called.");
        }

        public void ParentInterfaceMethod()
        {
            Console.WriteLine("ParentInterfaceMethod() called.");
        }
    }
}
 
The Output is:
 
img