Difference between Abstract class and Interface in C#

Introduction: In this article I am going to explain the difference between Abstract class and Interface in C# with suitable examples.

Abstract classes Vs Interfaces
An Interface is not a class. It is an entity that is defined by Interface. Within interface we can define signature of method but not declaration. An Abstract class is a special kind of class that can’t be instantiated. For more details please visit What is Abstract Class in C#. When, where and why to use it and What is Interface in C# and Where to use it .

There are several similarity between Abstract and Interface, Both are incomplete and can’t be instantiated and can be act as base type however there are several differences between Abstract classes and Interfaces. Let’s understand each point using suitable example.

1) Abstract classes can have implementations for some of its member because we know that we can put both implemented and unimplemented methods in Abstract classes while in Interfaces we can declare signature of method only, we can’t provide implementation of any method.

Example:
namespace ConsoleApplication
{
    public abstract class MyAbstractClass
    {
        public void Print()
        {
            Console.WriteLine("Print MyAbstractClass");
        }
    }

    interface MyInterface
    {
         void Print()  // It will show error "Interface members can't have definition." 
        {
            Console.WriteLine("Print MyInterface");
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
        }
    }
}    

   2) Interfaces members can’t have access modifier. All members are by default public.   Abstract class members have access modifier. By default they are public.

Example:
interface MyInterface
    {
        public void Print(); // It will show error "The modifier public is not valid for this item."        
     }
 

public abstract class MyAbstractClass
    {
        public void Print()// can have access modifier
        {
            Console.WriteLine("Print MyAbstractClass");
        }
    }

    3) An Abstract class can have fields and constants whereas Interface can’t have fields.

Example:
public abstract class MyAbstractClass
    {
        int Id; // can have fields
        public void Print()
        {
            Console.WriteLine("Print MyAbstractClass");
        }
    }


interface MyInterface
    {
        int Id; // It will show error "Interface can't contain fields." 
        void Print();       
    }


    4) An Interface can inherit from another interface only. Whereas Abstract class can inherit from another Abstract class or Interface.

    5) By using Interface we can achieve multiple inheritance, because a class can inherit from multiple interfaces at the same time, whereas a class can’t inherit from multiple classes at same time.

     6) An abstract class can have constructor declaration while an interface cannot do so.

Example:
public abstract class MyAbstractClass
    {
        public MyAbstractClass() // Constructor Declaration
        {
        }
    }

    interface MyInterface
    {
        public MyInterface() // It will show error "Interface can't contain constructors"
        {
        }       
     }


Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..