Click here to Skip to main content
14,971,553 members
Please Sign up or sign in to vote.
3.80/5 (3 votes)
See more:
When we have to use Abstract class and when we have to use Interface ?
Updated 4-Mar-10 1:46am

When appropriate.

If you are just learning, pick up a book and try using each of them.
Both delegates and interfaces allow a class designer to separate type declarations and implementation.

Interfaces describe a group of related behaviors that can belong to any class or struct. Interfaces can be made up of methods, properties, events, indexers, or any combination of those four member types. An interface can not contain fields. Interfaces members are automatically public.

A given interface can be inherited and implemented by any Class.

A delegate is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object.

A delegate can created for a method on any class, as long as the method fits the method signature for the delegate. An interface reference or a delegate can be used by an object with no knowledge of the class that implements the interface or delegate method. Given these similarities, when should a class designer use a delegate and when should they use an interface?
When to use a delegate:
• An eventing design pattern is used.
• It is desirable to encapsulate a static method.
• The caller has no need access other properties, methods, or interfaces on the object implementing the method.
• Easy composition is desired.
• A class may need more than one implementation of the method.
When to use an interface:
• There are a group of related methods that may be called.
• A class only needs one implementation of the method.
• The class using the interface will want to cast that interface to other interface or class types.
• The method being implemented is linked to the type or identity of the class: for example, comparison methods.

by msdn help
The sun could use an abstract class "any object"
(that is abstract, not concretely a stone, you or me)

and a general interface function "be warm"
(that could be differently implemented at each object of a concrete class).

The sun must not know any implemetation or its modification by you,
it say everyday just the same, at "any object": "be warm" :)
Interfaces are limited to public methods and constants with
no implementation. Abstract classes can have a partial
implementation, protected parts, static methods, etc.

A Class may implement several interfaces. But in case of
abstract class, a class may extend only one abstract class.
Interfaces are slow as it requires extra indirection to to
find corresponding method in in the actual class. Abstract
classes are fast.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900