Difference Between Abstract Class and Interface

Abstract Class is a class which cannot be instantiated. It may be partially implemented or unimplemented. The method which is not implemented in abstract class must be declare abstract also.

Have a look simple example:

abstract class MyClass
{
public void Method_Implemented()
{
//Your Code Here
}

public abstract void Method_NotImplemented();
}

Implementation of all methods must be given in class which is derived from abstract class same as interface. When we implement abstract method in derived class it must be marked as override.

Have a Look:
abstract class MyClass
{
public void Method_Implemented()
{
//Your Code Here
}
public abstract void Method_NotImplemented();
}

class ImplementClass : MyClass
{
public void Method_ClassOwnMethod()//Class own method
{
//Your Code Here
}
public override void Method_NotImplemented()//Abstract class method implementation
{
//Your Code Here
}
}

If we want to unimplement method of abstract class so the derived class also must declare as abstract class.

Have a look:
abstract class MyClass
{
public void Method_Implemented()
{
//Your Code Here
}
public abstract void Method_NotImplemented();
}

abstract class ImplementClass : MyClass
{
public void Method_ClassOwnMethod()//Class own method
{
//Your Code Here
}
//Not implement abstract class method
}

In above example derived class also mark as abstract because it not implement method of its parent abstract class.

Now what is the difference between abstract class and Interface?

1) Interface is only contain declaration of members (not code)whereas abstract class may contain code or not.

2) A class derived from multiple interfaces whereas class derived from at most one abstract class (As multiple inheritance not allowed in classes discussed in previous post).

3)Class restrict to implement all members of an interface whereas there is no restriction to implement all members in abstract class in that case the derived class must declare as abstract(Mentioned Above).

What is the purpose of abstract class?

OR

How we choose interface or abstract class for our scenarios?

When we want to build scenario in which all the classes have same structure but totally different implementation than we use interface.

When we want to build scenario in which all the classes have same structure and its implementation is different and have some same functionality.

For example the horse and cow both are herbivorous and both can speak but speak differently.

Example:
abstract class Animal
{
public void eat()//Same for horse and cow
{
Console.WriteLine("herbivorous");
}
public abstract void speak();//Only declaration because both speak differently
}

abstract class Horse : Animal
{
public override void speak()//Implementation according to Horse
{
//Your Code here
}
}

abstract class COW : Animal
{
public override void speak()//Implementation according to cow
{
//Your Code Here
}
}

Take a look diagrammatically:

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