wolfofthenorth
April 20th, 2003, 08:08 PM
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?
pareshgh
April 21st, 2003, 05:06 PM
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 (http://www.kuro5hin.org/story/2003/3/19/34653/8898)
Hope this clears your point.
- Paresh
wolfofthenorth
April 21st, 2003, 05:14 PM
pareshgh, Thank you!
That is exactly what I was looking for. :)
pareshgh
April 21st, 2003, 05:16 PM
do you like these teeth :D :D :D :D
wolfofthenorth
April 21st, 2003, 05:55 PM
The teeth are my icon of choice!
pareshgh
April 21st, 2003, 05:57 PM
COOL :cool: :D