CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2002
    Location
    Tatooine
    Posts
    155

    interface vs abstract base class?

    When or why would someone use an interface over an abstract or non abstract base class? The base class could implement some code, that an interface couldn't. So when would you ever choose to implement an interface over inheriting a base class?
    Last edited by wolfofthenorth; April 20th, 2003 at 10:53 PM.
    That which does not kill us, only makes us stronger.

    MCSD .NET

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    Hello,

    Just collected some stuffs from the net .
    Hope this will help you.



    <ABSTRACT CLASS>

    The choice of whether to design your functionality as an interface or an abstract class can sometimes be a difficult one. An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes.

    for more information look in abstract class details.

    <INTERFACE>

    An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.



    --------------------------------------------
    Both interfaces and abstract classes are useful for component interaction
    ---------------------------------------------



    a) Classes may inherit from only one base class, so if you want to use abstract classes to provide polymorphism to a group of classes, they must all inherit from that class.

    b) Abstract classes may also provide members that have already been implemented. Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.



    some recommendations for using whether Interface or Abstract classes.

    1) 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.


    2) 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.


    3) If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.


    4) 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


    FOR EXAMPLE:



    abstract class ICalculatorShim : ICalculator
    {
    abstract public int
    Add(int num1,int num2);

    abstract public int
    Subtract(int num1,int num2);

    abstract public int
    Divide(int num1,int num2);

    abstract public int
    Multiply(int num1,int num2);
    }





    check
    Interfaces v/s Abstract classes with respect to XML


    Hope this clears your point.
    - Paresh
    - Software Architect

  3. #3
    Join Date
    Nov 2002
    Location
    Tatooine
    Posts
    155
    pareshgh, Thank you!

    That is exactly what I was looking for.
    That which does not kill us, only makes us stronger.

    MCSD .NET

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    do you like these teeth
    - Software Architect

  5. #5
    Join Date
    Nov 2002
    Location
    Tatooine
    Posts
    155
    The teeth are my icon of choice!
    That which does not kill us, only makes us stronger.

    MCSD .NET

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    COOL
    - Software Architect

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured