none
When to use Interface instead of abstract class

    Question

  • tell me few scenario when people use Interface instead of abstract class.

    Interface and abstract class both pretty same with small difference that a concrete class can implement many interface but a class can extend single class or single abstract class.

    abstract class function could have body and could be abstract too but interface always have method signature only....no body.

    i saw many time people use interface say for example generic factory pattern or dependency injection, generic repository pattern are the few example where i have seen people prefer to use interface instead of abstract class.

    so anyone can tell me why people use interface when they work with generic factory pattern or dependency injection, generic repository pattern.....where as there we can use abstract class.

    looking for some discussion. thanks

    Monday, April 17, 2017 2:36 PM

Answers

  • Hi Mou,

    Have you seen below threads answer? They explains quite well. Hope this helps you.

    difference between abstract class and interface in c# with example

    My two cents on abstract class vs interface

    Abstract Class versus Interface



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Mou_kolkata Tuesday, April 18, 2017 9:40 AM
    Monday, April 17, 2017 3:19 PM
    Moderator
  • >why people use interface

    Interfaces support multiple inheritance, so are much less restrictive of implementers.

    Interfaces do not support class variables or methods, so implementation is also simpler.

    And finally because favoring Interfaces over Abstract Classes is the common C# idiom, so it makes your code easier to understand if you follow it.

    David


    Microsoft Technology Center - Dallas
    My blog

    • Marked as answer by Mou_kolkata Tuesday, April 18, 2017 9:40 AM
    Monday, April 17, 2017 8:06 PM
  • Hi Mou_kolkata,

    Thank you for posting here.

    The abstract class: In some classes, if we could abstract a base class and we need to write the method for these derived class, but we do not know how to write the method.

    The interface: We could not find the base class in some classes, but all of these classes have the same behavior and ability.

    >>so anyone can tell me why people use interface when they work with generic factory pattern or dependency injection, generic repository pattern.....where as there we can use abstract class.

    Please refer to the following links.

    https://www.codeproject.com/Articles/874246/Understanding-and-Implementing-Factory-Pattern-i

    https://www.codeproject.com/Tips/657668/Dependency-Injection-DI

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Tuesday, April 18, 2017 9:40 AM
    Tuesday, April 18, 2017 7:10 AM
    Moderator

All replies

  • Hi Mou,

    Have you seen below threads answer? They explains quite well. Hope this helps you.

    difference between abstract class and interface in c# with example

    My two cents on abstract class vs interface

    Abstract Class versus Interface



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Mou_kolkata Tuesday, April 18, 2017 9:40 AM
    Monday, April 17, 2017 3:19 PM
    Moderator
  • >why people use interface

    Interfaces support multiple inheritance, so are much less restrictive of implementers.

    Interfaces do not support class variables or methods, so implementation is also simpler.

    And finally because favoring Interfaces over Abstract Classes is the common C# idiom, so it makes your code easier to understand if you follow it.

    David


    Microsoft Technology Center - Dallas
    My blog

    • Marked as answer by Mou_kolkata Tuesday, April 18, 2017 9:40 AM
    Monday, April 17, 2017 8:06 PM
  • Hi Mou_kolkata,

    Thank you for posting here.

    The abstract class: In some classes, if we could abstract a base class and we need to write the method for these derived class, but we do not know how to write the method.

    The interface: We could not find the base class in some classes, but all of these classes have the same behavior and ability.

    >>so anyone can tell me why people use interface when they work with generic factory pattern or dependency injection, generic repository pattern.....where as there we can use abstract class.

    Please refer to the following links.

    https://www.codeproject.com/Articles/874246/Understanding-and-Implementing-Factory-Pattern-i

    https://www.codeproject.com/Tips/657668/Dependency-Injection-DI

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Tuesday, April 18, 2017 9:40 AM
    Tuesday, April 18, 2017 7:10 AM
    Moderator
  • the link you shared here i guess they will not tell me why people use interface when they code for generic factory pattern or dependency injection, generic repository pattern. so it would be better if you write few line to explain it. thanks
    Tuesday, April 18, 2017 9:40 AM
  • >generic factory pattern or dependency injection, generic repository pattern

    A sufficient reason is that your repository, or whatever may already have a base class.  For instance an Entity Framework repository class always has DbContext as its base class. 

    David


    Microsoft Technology Center - Dallas
    My blog

    Tuesday, April 18, 2017 10:33 AM