2

In my interview i was aked that give some realtime scenario where you can implement interface.. write some code also.
They want to ask we have abstract methods then why do we need interface... write some code.

Thanks in Advance

Chuck Norris
  • 14,368
  • 11
  • 83
  • 118
Pankaj Sharma
  • 65
  • 1
  • 2
  • 4
  • 1
    You have to do some search and write some code yourself first, then ask about a specific problem you have or for more explanation, no one will write this code for you. – Mahmoud Gamal Feb 23 '12 at 07:07
  • The information here will help you. Explained really well. http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo. – sinanakyazici Feb 23 '12 at 07:09
  • @JonSkeet - I'm in the middle of c# in depth, what a great book for someone that missed versions 1-2. – Induster Feb 23 '12 at 07:10
  • 1
    @Induster: Glad you're enjoying it :) – Jon Skeet Feb 23 '12 at 07:13

3 Answers3

1

Read this great documentation in MSDN:

Recommendations for Abstract Classes vs. Interfaces

Abstract Class versus Interface from Codeproject.com with sample code too.

Hope it helps!

uday
  • 7,922
  • 4
  • 28
  • 50
0

Because you can implement as many interfaces as you want. with an abstract class you can only inherit from one.

Induster
  • 743
  • 5
  • 15
0

As MSDN shows

  • If you anticipate creating multiple versions of your component,create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
  • If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
  • If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
  • If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
Chuck Norris
  • 14,368
  • 11
  • 83
  • 118