Wednesday 21 September 2011

What is Abstract Class? Why We Use Abstract Class?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:


Please feel free to ask.
Any corrections must be appreciable.
Thank You
hope it helps!

Happy Coding :)

7 comments:

  1. Is it make some difference if I make abstract class with all methods defined abstract and use it instead of interface and multiple inheritance is not required?

    ReplyDelete
  2. As you know there are multiple ways to do one thing.but we have to choose best.Interface is ideal for your scenerio

    ReplyDelete
  3. Good job, Shoaib!

    Regards,
    Samiullah

    ReplyDelete
  4. Nice Post. For more info you can visit: http://www.codinghub.net/2010/08/oops-in-c.html

    ReplyDelete
  5. I believe blogger has done something wrong with syntax highlighter. I am also suffering from this issue at http://zahidadeel.blogspot.com. Nice article however.

    ReplyDelete