Real example where we have to use interface ... not abstract class... write some code

20,655

Solution 1

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.

Solution 2

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!

Share:
20,655
Pankaj Sharma
Author by

Pankaj Sharma

Updated on July 09, 2022

Comments

  • Pankaj Sharma
    Pankaj Sharma almost 2 years

    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

  • phoog
    phoog about 12 years
    "interfaces...providing common functionality" -- somewhat misleading.