Pages

Wednesday 10 April 2013

Difference between an Abstract Class and Interface in C#

Difference between an Abstract Class and Interface in C#

Difference between an abstract class and interface is a very common concept and technical interview question. One must know this difference as its common in all the programming languages. I will try to make difference between abstract classes and interfaces with examples in C#.

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

Example of Interface in C#

using System;
interface IMyInterface
{
    void Read();
}
class Test : IMyInterface
{
    public void Read()
    {
      Console.WriteLine("Read");
    }
}
class Program
{
    static void Main()
    {
      IMyInterface myInterface = new Test(); // Create instance.
      myInterface.Read(); // Call method on interface.
    }
}

Output
Read

Example of Abstract Class in C#

using System;
abstract class Test
{
    public int _a;
    public abstract void A();
}
class Example1 : Test
{
    public override void A()
    {
      Console.WriteLine("Example1.A");
      base._a++;
    }
}
class Example2 : Test
{
    public override void A()
    {
      Console.WriteLine("Example2.A");
      base._a--;
    }
}
class Program
{
    static void Main()
    {
      // Reference Example1 through Test type.
     Test test1 = new Example1();
     test1.A();
     // Reference Example2 through Test type.
     Test test2 = new Example2();
      test2.A();
    }
}

Output
Example1.A
Example2.A

Conclusion

1. Multiple inheritance: A class may inherit several interfaces while a class may inherit only one abstract class.

2. You cannot declare any data member in an interface while you can do this in an abstract class.

3. You can only declare member functions but not define their bodies, but you can also define bodies of member functions. In case of abstract class you can declare and define bodies of member functions but atleast on member functions should be abstract which has no body.

4. An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public but in abstract class you have this flexibility.

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.