Click here to Skip to main content
15,885,941 members
Articles / All Topics

Interfaces Vs Abstract Classes

Rate me:
Please Sign up or sign in to vote.
4.58/5 (11 votes)
23 Sep 2011CPOL3 min read 42.3K   11   11
I have tried, to provide a difference between Interfaces and Abstract Classes

I remember my days when I used to get stuck on same question again and again and I know there are people who get stuck on this question, mostly freshers. The Big question is “What is the difference between Interface and Abstract Classes, which to use when ???”

So, I decided why not to write a post about it. If you Google for this you will find a lot of post on this topic, but the issue is that very few of them talk about which to use when. I will go slightly in technical details then I will talk about scenarios where you can use interface and scenarios where you can use Abstract Classes.

The Definitions

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So, why we need a class that cannot be instantiated? An abstract class is only to be 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 common behavior or properties across multiple classes who inherit the abstract class.

Declaration

public abstract class Employee
{
public virutal String CalculateSalary()
{
//Can provide default behavior
}
}

What is an Interface?

An interface is an entity that is defined by the word Interface. An Interface only contains signature of the methods whose implementation is to be provided by the classes who implements that Interface.

Declaration

public interface IEmployee
{
String CalculateSalary(); //Only Signature can be specified not even access modifiers.
}

Difference Between The Two

 Abstract ClassesInterfaces
Multiple InheritanceSince .NET does not support multiple inheritance, So A class can inherit only one Abstract Class

 

A class can implement multiple Interfaces.

 

Default Behavior

 

In abstract class you can provide default behavior of a function, so that even if child class does not provide its own behavior, you do have a default behavior to work with. Eg. Abstract classes of framework, even if you don’t provide your own behavior, they have a default behavior

 

You cannot provide a default behavior in interfaces. Interfaces only allow you to provide signature of the method.

 

Access Modifiers

 

You can provide access modifiers to methods in abstract classes.

 

You cannot provide access modifiers methods in Interfaces.

 

Scenario

Now, lets talk about a single scenario that I hope it will make you understand the situations where to use what.

Lets Assume you need to make three classes, first is CAR, second is MAN, third is WOMAN. Now you need a function in each of them to define how they Move. Now all three can move but CAR moves entirely in different way than MAN and WOMAN. So here we use an Interface IMOVEMENT and declare a function MOVE in it. Now all three classes can inherit this interface. So the classes goes like this.

public interface IMovement
{
void Move();
}

public class Car : IMovement
{
public void Move()
{
//Provide Implementation
}
}

public class Man : IMovement
{
public void Move()
{
//Provide Implementation
}
}

public class Woman : IMovement
{
public void Move()
{
//Provide Implementation
}
}

But, since MAN and WOMAN walk in similar way, so providing same behavior in two different methods will be code redundancy, in simpler words code is not re-used. So we can now define a Abstract Class for Human Beings movements, so this class can be HUMANBEINGMOVEMENT. Also the same can be applied to CAR class, since there are lot of manufactures for cars and all cars move in similar way so we can also define a abstract class for Cars movement which can be CARSMOVEMENT. So our refactored code will be .

public interface IMovement
{
void Move();
}

public abstract class CarsMovement : IMovement
{

public virtual void Move()
{
//default behavior for cars movement
}
}

public class SuzukiCar : CarsMovement
{
public override void Move()
{
//Provide Implementation
}
}

public abstract class HumanBeingMovement : IMovement
{

public virtual void Move()
{
//default behavior for human being movement
}
}

public class Man : HumanBeingMovement
{
public override void Move()
{
//Provide Implementation
}
}

public class Woman : HumanBeingMovement
{
public override void Move()
{
//Provide Implementation
}
}

Now as you can see, an hierarchy is appearing now every human being can inherit from our HUMANBEINGMOVEMENT class for the move method and all types of cars can inherit from our CARSMOVEMENT class for the move method.

I hope this post made you a little more clear about interfaces and abstract classes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice Article Pin
Amey K Bhatkar25-Mar-14 19:02
Amey K Bhatkar25-Mar-14 19:02 
AnswerRe: Nice Article Pin
Sumit_Gupta18-Aug-14 21:47
Sumit_Gupta18-Aug-14 21:47 
GeneralMy vote of 5 Pin
pratipgd4-Dec-12 23:06
pratipgd4-Dec-12 23:06 
GeneralRe: My vote of 5 Pin
Sumit_Gupta20-Dec-12 20:48
Sumit_Gupta20-Dec-12 20:48 
GeneralMy vote of 4 Pin
jxzg28-Sep-11 20:18
jxzg28-Sep-11 20:18 
GeneralMy vote of 4 Pin
wincsi27-Sep-11 1:16
wincsi27-Sep-11 1:16 
GeneralGreat article Pin
norrisMiou26-Sep-11 14:26
norrisMiou26-Sep-11 14:26 
Great article and thanks for it !
Since a long time I try to understand the "why" and "when" of interfaces, and that's the first time I really understand the "when"! You opened my mind on another style of architecture for my applications.
Thanks.
Sorry for my english

GeneralRe: Great article Pin
Sumit_Gupta26-Sep-11 18:46
Sumit_Gupta26-Sep-11 18:46 
QuestionVery Minor mistake Pin
maq_rohit22-Sep-11 23:46
professionalmaq_rohit22-Sep-11 23:46 
AnswerRe: Very Minor mistake Pin
Sumit_Gupta23-Sep-11 1:32
Sumit_Gupta23-Sep-11 1:32 
GeneralMy vote of 4 Pin
Reiss22-Sep-11 1:01
professionalReiss22-Sep-11 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.