none
Best Situation that describe difference between Interface and Abstract

    Question

  • Hi, Please tell me the best example or situation where we should choose interface or abstract class?
    Monday, May 16, 2011 9:42 AM

Answers

  • It's depends upon your design pattern. Often I used as

    1) An abstract class with some non abstract methods as Base Class

    2) Mutiple interfaces based on the common requirement throughout the application flow.

    Thanks


    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Monday, May 16, 2011 10:10 AM
  • Use an Interface when you need a class to inherit from 2 base classes or more.

    You will not be able to inherit from 2 abstract classes, but you can inherit from a class and as much interfaces as you need.

     

    Use an abstarct class when you need your derived class to inherit from only one class.

    Also, when you want to put a code into the base class - use an abstract class.

    It is impossible to put code into an Interface.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Monday, May 16, 2011 10:48 AM
  • Take a look at this article, it discusses not only the detailed difference from the aspect of language defination, but how to decide which to use in one specific situation or requirement.
    http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 5:43 AM
    Moderator
  • Smiley,

    It is the same as about apples and computers.

    An Interface is a contract, it describes what a class should contain to implement it (and can than be used to to get those members of a class which fulfill the contract) but an interface contains no actual implementation.

    An Abstract Class is a class itself which can be (only) inherited used as base for another class, the members and properties of a base class can be used likewise the inheriting class. 

    Because of above can an Abstract Class implement an Interface, however an Interface cannot implement an Abstract class. 

     


    Success
    Cor

    Tuesday, May 17, 2011 6:23 AM

All replies

  • It's depends upon your design pattern. Often I used as

    1) An abstract class with some non abstract methods as Base Class

    2) Mutiple interfaces based on the common requirement throughout the application flow.

    Thanks


    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Monday, May 16, 2011 10:10 AM
  • Use an Interface when you need a class to inherit from 2 base classes or more.

    You will not be able to inherit from 2 abstract classes, but you can inherit from a class and as much interfaces as you need.

     

    Use an abstarct class when you need your derived class to inherit from only one class.

    Also, when you want to put a code into the base class - use an abstract class.

    It is impossible to put code into an Interface.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Monday, May 16, 2011 10:48 AM
  • Take a look at this article, it discusses not only the detailed difference from the aspect of language defination, but how to decide which to use in one specific situation or requirement.
    http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 5:43 AM
    Moderator
  • Smiley,

    It is the same as about apples and computers.

    An Interface is a contract, it describes what a class should contain to implement it (and can than be used to to get those members of a class which fulfill the contract) but an interface contains no actual implementation.

    An Abstract Class is a class itself which can be (only) inherited used as base for another class, the members and properties of a base class can be used likewise the inheriting class. 

    Because of above can an Abstract Class implement an Interface, however an Interface cannot implement an Abstract class. 

     


    Success
    Cor

    Tuesday, May 17, 2011 6:23 AM