Difference Between Interface vs Abstract Class

From JholJhapata
Revision as of 06:58, 5 January 2020 by Admin (talk | contribs) (Created page with "Interface and Abstract Class, both are the ways to achieve the abstraction in C#. == Abstract Class == An '''Abstract class''' is never intended to be instantiated directly. T...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Interface and Abstract Class, both are the ways to achieve the abstraction in C#.

Abstract Class

An Abstract class is never intended to be instantiated directly. This class must contain at least one abstract method, which is marked by the keyword abstract in the class definition. The Abstract classes are typically used to define a base class in the class hierarchy.

  abstract class Software
  {
      // Abstract method (does not have a body)
      public abstract void addIcon();
      // Regular method
      public void defaltPath()
      {
          Console.WriteLine("c:");
      }
  }
  // Derived class (inherit from Software)
  class WinZip : Software
  {
      public override void addIcon()
      {
          // The body of addIcon() is provided here
          Console.WriteLine("Desktop");
      }
  }
  class Program
  {
      static void Main(string[] args)
      {
          WinZip myWinZip = new WinZip(); // Create a WinZip object
          myWinZip.defaltPath();  // Call the abstract method
          myWinZip.addIcon();  // Call the regular method
      }
  }

Interface

Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly.

  interface Software
  {
      // Abstract method (does not have a body)
      void addIcon();
      // Regular method
      void defaltPath();
  }
  // Derived class (inherit from Software)
  class WinZip : Software
  {
      public void addIcon()
      {
          // The body of addIcon() is provided here
          Console.WriteLine("Desktop");
      }
      public void defaltPath()
      {
          Console.WriteLine("c:");
      }
  }
  class Program
  {
      static void Main(string[] args)
      {
          WinZip myWinZip = new WinZip(); // Create a WinZip object
          myWinZip.defaltPath();  // Call the abstract method
          myWinZip.addIcon();  // Call the regular method
      }
  }

Difference between Abstract Class and Interface

ABSTRACT CLASS INTERFACE
It contains both declaration and definition part. It contains only a declaration part.
Multiple inheritance is not achieved by abstract class. Multiple inheritance is achieved by interface.
It contain constructor. It does not contain constructor.
It can contain static members. It does not contain static members.
It can contain different types of access modifiers like public, private, protected etc. It only contains public access modifier because everything in the interface is public.
The performance of an abstract class is fast. The performance of interface is slow because it requires time to search actual method in the corresponding class.
It is used to implement the core identity of class. It is used to implement peripheral abilities of class.
A class can only use one abstract class. A class can use multiple interface.
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class. If many implementations only share methods, then it is superior to use Interface.
Abstract class can contain methods, fields, constants, etc. Interface can only contain methods.
It can be fully, partially or not implemented. It should be fully implemented.