Monday, September 13, 2010

Difference between abstract class and interfaces in .net

Many a times we encounter this question in our minds when deciding on what to choose between abstract class or interface while implementing projects. Further many not having clear idea of the pros and cons of both just choose either of them for implementation. In this post I am going to throw some light on the difference between abstract class and interface in .net and how to choose between the two.

Abstract Class

These are those classes which cannot be instantiated i.e. their objects cannot be made. We can declare a class abstract by using the keyword abstract before the class keyword.

abstract class ABC
{
.......
}

The important point here to note is that all OOPS concepts apply to the abstract class i.e. whatever we can do within a class can be done here too, like declaring a variable, creating a function, function overloading, function overriding, access modifiers (public, private, protected), inheritance, constructor, destructor, properties, events , indexers, etc. All these concepts can be implemented in a abstract class.

A abstract class can inherit from some other class also:

class A
{.....}

abstract class B : A
{.....}

This means that within a inheritance hierarchy the position of abstract class can be anywhere except the last class i.e. the last child class.

If inheritance is in this order A - >B - > C - > D - > E then all classes except class E can be abstract class.

Further a abstract class can implement interfaces also:

interface I
{......}

abstract class B : I
{.....}

An abstract class can implement combination of both class and interface also, but only one class and any number of interfaces


abstract class B : MyClass, I1, I2, I3, I4
{.....}

Another important point is that if we have declared a constructor in abstract class, it will be called whenever the child class object is made.

class A
{
    public A()
     {
           Console.WriteLine("hello");
      }
...}

abstract class B : A
{.....}

B ob1 = new B();

output will be: hello


Interfaces

Interfaces only provide with the declaration of methods, properties, events, indexers, or any combination of those four member types only. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers. Also Interfaces contain no implementation of methods.

Interface MyInter
{
     void Check();
}

A interface can inherit only from any number of interfaces but not from classes. Further an interface cannot be instantiated directly.





2 comments:

  1. It really helped me out in searching the various sp and views that are using a particular table especially considering that the DB is having more than 4200 sp's ! Thanks....

    ReplyDelete
  2. Hi Jay
    You have explained it really well. I am an IT student and it really helped me to understand about abstract class and interface. Good on you.

    ReplyDelete

Comments to this post

LinkWithin

Related Posts with Thumbnails