Sunday, January 6, 2013

Abstract Class vs Interfaces (C#) - Differences, Recommendations about when to use what?

This is a favorite question of any interviewer. Probabilities are less where interviewer expects syntactic differences rather one might be looking at design level differences. Next question which follows is "When do you prefer interface over abstract classes" or other way round. I have collected answers over the internet to compile one simple page to answer this question.

Below is from John Noah's interview questions comiplation:


An abstract class can provide complete, default
code and/or just the details that have to be
overridden.

An interface cannot provide any code at all,just
the signature.

In case of abstract class, a class may extend
only one abstract class.

A Class may implement several interfaces.

An abstract class can have non-abstract
methods.

All methods of an Interface are abstract.

An abstract class can have instance variables.
An Interface cannot have instance variables.

An abstract class can have any visibility:
public, private, protected.

An Interface visibility must be public (or)
none.

If we add a new method to an abstract class
then we have the option of providing default
implementation and therefore all the existing
code might work properly.

If we add a new method to an Interface then
we have to track down all the implementations
of the interface and define implementation for
the new method.

An abstract class can contain constructors .
An Interface cannot contain constructors .

Abstract classes are fast.

Interfaces are slow as it requires extra
indirection to find corresponding method in the
actual class.


Below answers are from MSDN.

When shall we prefer Abstract classes or Interface over other? 

  • 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.

  • 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.

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

  • 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.


6 comments:

  1. Good explanation. Interface have no super most interfaces.Abstract class have super most (root) class called Object.


    prathap
    dot net training in chennai

    ReplyDelete
  2. Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...
    python training Course in chennai
    python training in Bangalore
    Python training institute in bangalore

    ReplyDelete
  3. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS Certification Training Online Toronto
    AWS Certification Training Online Newyork
    AWS Certification Training Online London

    ReplyDelete
  4. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    AWS training in chennai

    AWS Training in Bangalore

    ReplyDelete