Saturday, May 2, 2015

Abstract Class Vs Interface in C#.Net


Abstract Class Vs Interface in C#.Net

Memebers Allowed

Abstract as well as non abstact allowed.

Only abstract members are Allowed.

Implementation of methods in derived classes

Not necessary to implement all the methods of base (abstract) class for derived classes.

The derived class must implement all the methods of interface.

Fields

Can be defined.

Can not be defined.

Access Modifier

Can contain access modifiers for the functions, properties.

Cannot have access modifiers for the functions, properties all members of interface is assumed as public.

Advantage of Interface over Abstract class
A class may inherit only one abstract class. A class may inherit several interfaces.

Advantage of Abstract class over Interface
Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.

Now explanation of above points as below,

Members Allowed & Fields

Abstract class with abstract as well as non-abstract members as below,



Interface allows only abstract members (i.e. methods with declaration only not implementation) as below,


Access Modifier and Implementation of methods in derived classes

                                                                                   Interface implementation in Derived classes




Abstract Class implementation in derived classes



3 comments:

  1. Memebers Allowed for interface only abstract members ,Update The First Item In comparison

    ReplyDelete
    Replies
    1. @AhmedEzzat - Thanks for your input. It was typo mistake, While explanation i have written correctly but in tabular format it was typo mistake.

      Delete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. Dot Net Training in chennai |dot net training institute in velachery

    ReplyDelete