articles

Home / DeveloperSection / Articles / Difference Between Interface vs Abstract Class in C#

Difference Between Interface vs Abstract Class in C#

Anupam Mishra 7658 17-Dec-2015

Interface and Abstract class both are using to achieve Abstraction in C#.Interface are achieving fully (100%) abstraction but abstract class is partially (0-100%).

Interface provide only declaration not definition but abstract class using both. We can’t declared member in interface. In interface all methods are public. In Interface we can’t defined keyword static virtual, abstract or sealed. We have not create any instance of abstract class. If any class is one method are abstract then that class must be make as abstract. Abstract class is using for providing common definition of a base class that multiple derived class can share.  

We have achieved multiple inheritance by using interface in c#. If base class is an abstract class then derived class inherit only one abstract class at a time.i.e. We will not achieving multiple inheritance using abstract class. But using interface we will accomplish this task very quickly.

For Example ,

using System;
 
namespace InterfaceEx
{
   //declaring interface
 
   public interface Bank
    {
       void RateOfInterest();
    }
 
  //defining class that implement Bank interface
  public class PNB:Bank
    {
      //Given body to the interface method
      public void RateOfInterest()
        {
            Console.WriteLine("Interest Rate of PNB is : 7 %");
        }
    }
 
  //defining class that implement Bank interface and containing main method
   public  class SBI:Bank
    {
       //Given body to the interface method
      public void RateOfInterest()
       {
    Console.WriteLine("Interest Rate of SBI is : 6 %");
       }
      
       static void Main(string[] args)
        {
           // creating instance of the class
            PNB pnb = new PNB();
   //call class method which are given body that are defined in interface
            pnb.RateOfInterest();
            SBI p = new SBI();
            p.RateOfInterest();
            Console.ReadKey();
 
 
        }
    }
}
 
For Abstract class:
using System;
 
namespace AbstractClassEx
{
 
abstract class Car
    {
      //normal method
       public void setName()
        {
            Console.WriteLine("New Car Name is: Swift Dezire Turbo");
        }
       //virtual function that override in derived class
public virtual void setFeature()
        {
            Console.WriteLine("Dramastic changes in Feature");
       
        }
       //abstract method
        public abstract void Design();
}
 
  class Program:Car
    {
       //Giving body to abstract method
      public override void Design()
       {
           Console.WriteLine("Degisns very New & Energetic");
 
       }
//override virtual method 
     public override void setFeature()
       {
           Console.WriteLine("Its Internal Feature");
       }
 
       static void Main(string[] args)
        {
              //Creating instancs
            Program p = new Program();
           //calling abstract class method
      p.setName();
            p.Design();
            p.setFeature();
Console.ReadKey();
        }
    }

c# c# 
Updated 14-Dec-2017

Leave Comment

Comments

Liked By