14 June 2011

Interface vs Abstract Class in C#

Interface vs Abstract Class

Multiple inheritance

Interfaces:A class may inherit several interfaces.
Abstract Class:A class may inherit only one abstract class.

Default implementation

Interfaces:An interface cannot provide any code, just the signature.
Abstract Class:An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modifiers

Interfaces:An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
Abstract Class:An abstract class can contain access modifiers for the subs, functions, properties

Core VS Peripheral

Interfaces:Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
Abstract Class:An abstract class defines the core identity of a class and there it is used for objects of the same type.

Usage in Application

Interfaces:If various implementations only share method signatures then it is better to use Interfaces
Abstract Class:If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed

Interfaces:Requires more time to find the actual method in the corresponding classes.
Abstract Class:Compare to Interface it is very fast

Fields and Constants

Interfaces:No fields can be defined in interfaces
Abstract Class:An abstract class can have fields and constrants defined


Main Limitation of Interface

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
in all derived class.

Main Limitation of Abstract Class

A class may inherit only one abstract class.

5 comments:

  1. Indeed you have done a good job on differentiating the interface and abstract class in java . just to add when we use abstract class we can not extend another class but with interface we have that space spare with us. just consider if Runnable were an abstract class instead of interface, how inflexible it would be.

    Javin
    How HashMap works in Java

    ReplyDelete
    Replies
    1. Both usage is little bit different.

      Interfaces are best suited for providing common functionality to unrelated classes.

      Abstract class are best suited for providing common functionality to related classes.

      Based up on the needs we can use Interface or Abstract class.

      Delete
  2. In Java and C# We implement the multiple inheritance by using Interface...Otherwise Abstract class is preferable . speed wise Abstract class is better one.

    ReplyDelete
  3. Thank you so much for giving us detailed knowledge about interface vs abstract class in java.
    keep posting like that.
    thanks

    ReplyDelete

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...