Home > .NET, VB.Net, C#, Diiferences > Interface vs Abstract Class

Interface vs Abstract Class

Technical Differences

S.No. Interface Abstract Class
1. Consists of only four type of members

(1) Method (2) Property (3) Event (4) Indexer

As a normal class it can have all supported members
2. Declaration only allowed. There will not be any implementation or definition (i.e., can not have virtual and concrete members and only abstract members are allowed)

It supports

1) Abstract members (only declaration)

2) Virtual members (with definition)

3) Concrete members (with definition)

3. No access modifiers can be used. By default members are abstract and public Modifiers allowed and to define abstract member ‘abstract’ keyword is used
4. A class that inherits an interface should implement all the members defined in the interface. No ‘override’ keyword is used but ‘public’ keyword should be used. A class that inherits an abstract class should implement all abstract members defined in the abstract class. ‘override’ keyword is used.

For virtual members, the class can either override or not.

5. A class can inherit more than one interfaces A class can not inherits more than one abstract class
6. A struct can inherit interfaces A struct can not an abstract class

Usage and Functional Differences

S.No. Interface Abstract Class
1. As the name implies, it generally used as a contract for classes.  It only can define signatures not the implementations that the derived classes should implement the same. Abstract class offers variety in its implementation and it is generally used to encapsulate common functionalities for both all derived classes and particular derived classes.
2. It is well suitable for sending an object via interface to ensure ‘data hiding’ because only the members defined in interface can be accessed from the interface all other members will be hidden. It is well suitable for the situation to extend common functionalities (versioning).  Adding a common method in the base abstract class is enough and it will automatically make all the derived classes have the new method.
3. In structure inheritance, when casting the structure to interface object, a temporary object will be created by boxing (value type to reference type) and making any changes in interface object will not affect the original struct object. Since no boxing required (because both class and interface are reference types), making any changes in interface object will affect the original struct object. This is because; both are same object with two references.

Technical Difference

Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.