Abstract Class vs Interface

Posted by Techie Cocktail | 8:54 AM | | 0 comments »

This has always been a favorite topic in the interviews. Checkout some of their differences.



Abstract

a. An abstract class cannot be instantiated. To use the abstract class methods it needs to be derived and used.

b. Unlike interfaces, abstract class can contain constructors or destructors, fields, define or implement methods, properties.

c. Abstract class dont support multiple inheritance.

d. Abstract class supports public, private, internal access specifiers.

e. Abstract class are faster in performance compared to interfaces.



Lets see an example of abstract class implementation:



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace ConsoleApplication3

{

class Program

{

public abstract class vehicle

{

public abstract void design();

public virtual void speed()

{

Console.WriteLine("Test the speed of the car");

}

}



public class sedan : vehicle

{

//Note if the design() class is not implemented here then you get an error.

public override void design()

{

Console.WriteLine("4 door design model");

}

}



static void Main(string[] args)

{

sedan _sedan = new sedan();

_sedan.speed();

_sedan.design();

Console.Read();

}

}

}



In the above example, vehicle class is an abstract class. It is defined using abstract keyword. It has two methods - design() & speed(). The design() method is abstract and is generic in nature. It imposes on the derived class to implement the design() method as per its purpose in the sedan class or any other derived class. The speed() method has the default implementation in the vehicle class and is virtual in nature. This means that the derived class can either inherit the speed() method as is in its base class or can override the speed method to update its implementation.



Interface

a. An interface on the otherhand can contain only abstract members. It cannot have method implementation, just the method definitions. The class that implements the interface must implement all the methods defined in that interface.

b. The interface cannot contain constructors or destructors, fields, or properties. And by default all the method definitions are public in nature.

c. The class can implement one or more interfaces thereby supports multiple inheritance.

d. Interface does not support any access modifiers like public, private.

e. Interface are comparitively slower in performance. It takes more time to find the corresponding class containing the requested method.



Lets see an interface example,



public interface IVehicle

{

void design();

void speed();

}



public class coupe : IVehicle

{

public void design()

{

Console.WriteLine("2 door design model");

}



public void speed()

{

Console.WriteLine("Test the speed of the car");

}

}



In the above example, IVehicle is the interface. The method definitions are by default public in nature and the class that implements this interface should declare the methods as public. Here the coupe class implements the methods that are mandated by the interface to use them in the class that implements it.



If you want to decide to choose between using an interface or an abstract class, refer MSDN for recommendations.

0 comments