.Net Interview Questions – Differences between Abstract Class and Interface

This question appears almost in every interview for .NET developer role. It is very important that we clearly list the differences between them, explain the concepts behind each of these differences, theoretical and practical concepts behind the implementation. And finally we can give an opinion on which to use when and why.

What is an Abstract Class?

First, let’s explain clearly what is an abstract class. An Abstract class provides a model for his derived classes. It means that in an abstract class we define a framework for derived classes, functionality (which can be overwritten or defining contracts that must be implemented). It is important to say that you can’t create an instance of an abstract class so if you want to use an abstract class you must create a derived non-abstract class. There are two ways to declare functionality in an abstract class as we can see in the following example.

public abstract class Vehicle
{
    virtual public string Run ()
    {
        return "I'm working";
    }

    abstract public string Stop ();
}

public class Car: Vehicle
{
    public override string Stop ()
    {
        return "just stop";
    }
}

As we can see in our abstract class we declared two methods, the method Run uses the  keyword virtual, this is used to define a method that has a defined basic behavior but leaves the door open for the class that derives from this can overwrite this behavior. The method Stop in the other hand uses the reserved keyword abstract, this method doesn’t have a predefined  behavior. This allow us define a contract that must be implemented in the derived class as shown in the example above. Thus two ways in which we can use an abstract class and is to define behavior or to define a contract.
There is something to be stressed at this point that abstract classes are governed by rules heritage with which a class can only inherit from a class (or abstract ).

What is an Interface?

Interfaces are a resource that contains a definition of functionalities that must be implemented in a class. It is important to highlight that an interface does not define behavior, the same whether or must be defined in classes that implement it. Those classes must define behavior for all members of the interface. Because the interfaces are not classes a class can implement as many interfaces as desired as can be seen in the following example.

public interface IDraw
{
   string Draw ();
   string Set ();
}

public interface IDrawHexagon
{
   string Draw ();
}

public class Hexagon: IDraw, IDrawHexagon
{

   string IDraw.Draw ()
   {
      return "Draw method implementation" ;
   }

   public string Set ()
   {
      return "Method implementation Set" ;
   }

   string IDrawHexagon.Draw ()
   {
      return "Draw method implementation Vertices" ;
   }
}

 

You can see our class implements two interfaces. This behavior is not allowed with classes, a class cannot inherit from more than one class because C# doesn’t support multiple inheritance.

You can realize the method Draw is defined by both Interfaces. For this reason in our class Hexagon we need to explicit to which interfaces the method we are defining belongs. We can define which method we want to use implementing a cast.

hexa var = new Hexagono ();
IDraw test1 = (IDraw) hexa;
Console.WriteLine (test1.Draw());
IDrawHexagon test2 = (IDrawHexagon) hexa;
Console.WriteLine (test2.Draw());

The differences

To sum up we can say that either Abstract Classes or Interfaces allow us to define behavior. Using Interfaces we can implement more than one, in the other hand a class can inherit from only one class (abstract or not). A down side of the Interfaces is that the implementations are “all or nothing” which means that when you are implementing an interface must specify all the methods defined in the interface. As we said before Interfaces doesn’t define basic behavior to the method, only the signature.

To wrap up we can define the following rules to remember the diferences:

  • When a class implement an Interface answer the question: “it works like …”
  • When a class inherit from another class answer the question: “it is a ..”
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s