Monday, August 4, 2014

Differences Between Interfaces and Abstract Classes

Differences Between Interfaces and Abstract Classes

C#

An abstract class is "which is lack of implementations". Means it doesn't keep it complete implementations.

And if you want to perform some "protocols(some method to be execute)" at application level than we should follow an interface. 

  1. A class may implement several interfaces but can only extend one abstract class.
    An interface cannot provide any code, just the signature.An abstract class can provide complete,just the details that have to be overridden.
  2. If various implementations only share method signatures then it is better to use Interfaces.
    If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
  3. An interface cannot have access modifiers for the subs, functions, properties etc. everything is assumed as public
    An abstract class can contain access modifiers for the subs, functions, properties
  4. Abstract Class may contain constructor but interface does not contain constructor.
    Very effective example of both like below:
    Suppose we have an interface that has method like "Android" which is applicable for all mobile devices like Samsung, Micromax, Karbonn. which makes satisfactory for this line "If various implementations only share method signatures then it is better to use Interfaces"
    While if you have method named as "Galaxy" than would be applicable only for Samsung abstract class which makes satisfactory for line."If various implementations are of the same kind and use common behavior or status then abstract class is better to use".
  5. 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.
    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.(e.g)

We can either add concrete method or abstract method.

 

To know more about MVC please go through with given below link.

MVC Articles

Enjoy Coding and Reading Smile

0 comments :

Post a Comment