2

Possible Duplicate:
When to use an interface instead of an abstract class and vice versa?
Interface vs Abstract Class (general OO)

I have

public interface MyInterface
{
  void Execute();
}

And

public abstract class MyAbstractClass
{
  public abstract void Execute();
}

Now what will be the difference between the abstract class and the interface when a class will implement them? When and what will be the reason for a class to implemenet them when an abstract class has only one abstract method?

This question was asked to me yesterday in an interview..I failed to answer..

Thanks in advance

Community
  • 1
  • 1
  • 1
    possible duplicate of [When to use an interface instead of an abstract class and vice versa?](http://stackoverflow.com/questions/479142/when-to-use-an-interface-instead-of-an-abstract-class-and-vice-versa) also also: http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo?rq=1 and lots of others... – nemesv Jul 20 '12 at 05:09
  • @nemesv - not a duplicate - this is subtley different. Normally, one would use an abstract class to provide implementation, but OP here states that there is no implmentation. – StuartLC Jul 20 '12 at 05:12
  • 5
    There's a conceptual difference that is obvious. Also, remind that a class inherit from 1 only, but may implement N interfaces. – Andre Calil Jul 20 '12 at 05:13
  • The key is understanding the difference between a "class", an "abstract class" and an "interface". The "one abstract method" part is a red herring. IMHO... – paulsm4 Jul 20 '12 at 05:15

2 Answers2

5

Even when this is a duplicate I will try to clarify it for you:

Now what will be the difference between the abstract class and the interface when a class will implement them?

If you use an abstract class, the difference is that you would need to inherit from your abstract class and then override the abstract method.

If you use an interface, you would need to implement the method, without inherit from this class, giving you the flexibility to inherit from another class if needed

When and what will be the reason for a class to implemenet them when an abstract class has only one abstract method?

Do not think about the number of methods in order to choose between one or the other, think about the trade-offs you would get when using one against the other.

  • If you decide to use an abstract class, your derived classes will have to inherit form your abstract class in order to work, and since you only can inherit from one single class in C#, you will be tight to the abstract class.

  • If you implement an interface, you will be able to inherit from another class hierarchy, in other words, using interfaces will give you the most flexible design

Now abstract class are really useful when you want to write a base piece of code, that your child classes will inherit, in that case, you would be writing less code in your child classes efectivley reusing the code written in your abstract class. Also you will be able to override the abstract methods to supply your own implementation

Jupaol
  • 21,107
  • 8
  • 68
  • 100
1

As we know there are many ways in oop to implement one thing.But we have to follow best practices or use specialized methods.

In your scenario there is no big difference in interface and abstract class.but you have to go for concepts of difference between the interface and abstract. here is the nice article : Difference Interface and Abstract Class

SMK
  • 2,098
  • 2
  • 13
  • 21