Click here to Skip to main content
Click here to Skip to main content

Tagged as

Interfaces Vs Abstract Classes

, 23 Sep 2011
Rate this:
Please Sign up or sign in to vote.
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 Classes Interfaces
Multiple Inheritance Since .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)

Share

About the Author

samrules
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
QuestionNice Article PinpremiumAmey K Bhatkar25-Mar-14 19:02 
AnswerRe: Nice Article Pinmembersamrules18-Aug-14 21:47 
GeneralMy vote of 5 Pinmemberpratipgd4-Dec-12 23:06 
GeneralRe: My vote of 5 Pinmembersamrules20-Dec-12 20:48 
GeneralMy vote of 4 Pinmemberjxzg28-Sep-11 20:18 
GeneralMy vote of 4 Pinmemberwincsi27-Sep-11 1:16 
GeneralGreat article PinmembernorrisMiou26-Sep-11 14:26 
GeneralRe: Great article Pinmembersamrules26-Sep-11 18:46 
QuestionVery Minor mistake Pinmembermaq_rohit22-Sep-11 23:46 
AnswerRe: Very Minor mistake Pinmembersamrules23-Sep-11 1:32 
GeneralMy vote of 4 PinmemberReiss22-Sep-11 1:01 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 23 Sep 2011
Article Copyright 2011 by samrules
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid