Sunday, 13 December 2009

Where to use Interface and where to use class?


There are many articles on “Interface Vs Abstract Classes”, here I would like to share that same thing in simple words. This article will describe some similarities and differences between both. And to answer the most important question,where and when to use Interface?


In both C# and Visual Basic 2005, the reserved word interface defines a CLR reference type.

  1. Interfaces can’t have any implementation; Saying that an interface can’t have implementation means that the entire interface’s methods and properties were abstract.
  2. Can’t be instantiated, Saying it can’t be instantiated means the same as if the interface were an abstract class (or MustInherit in Visual Basic 2005)
  3. And has only public members... For example, this interface definition:

public interface IMyInterface

{

void Method1( );

void Method2( );

void Method3( );

}

Is almost equivalent to this class definition:

public abstract class MyInterface

{

public abstract void Method1( );

public abstract void Method2( );

public abstract void Method3( );

}


In traditional object-oriented programming, you typically use an abstract class to define a service abstraction. The abstract class serves to define a set of signatures that multiple classes will implement after deriving from the abstract class. When different service providers share a common base class, they all become polymorphic with that service abstraction, and the client can potentially switch between providers with minimum changes.


There are a few important differences between an abstract class and an interface:

  1. An abstract class can still have implementation: it can have member variables or non-abstract methods or properties. An interface can’t have implementation or member variables.
  2. An abstract class can derive from any other class or from one or more interfaces. An interface can derive only from other interfaces.
  3. An abstract class can have nonpublic (protected or private) methods and properties, even if they are all abstract. In an interface, by definition, all members are public.
  4. An abstract class can have static methods and static members and can define constants. An interface can have none of those.
  5. An abstract class can have constructors. An interface can’t.


Where to use Interface and where to use class?


These differences are deliberately in place, not to restrict interfaces, but rather to provide for a formal public contract between a service provider (the classes implementing the interface) and the service consumer (the client of the classes). Disallowing any kind of implementation details in interfaces (such as method implementations, constants, static members, and constructors) enables .NET to promote loose coupling between the service providers and the client. Because there is nothing in the contract that even hints at implementation, by definition the implementation is well encapsulated behind the interface, and service providers are free to change their implementations without affecting the client. You can even say that the interface acts like a binary shield, isolating each party from the other.



Because interfaces can’t be instantiated, .NET forces clients to choose a particular implementation to instantiate. Having only public members in an interface complements the contract semantics nicely: you would not want a contract with hidden clauses or "fine print." Everything the contract implies should be public and well defined. The more explicit and well defined a contract is, the less likely it is that there will be conflicts down the road regarding exactly what the class providing the service is required to do. The class implementing the interface must implement all the interface members without exception, because it has committed to providing this exact service definition. An interface can extend only other interfaces, not classes. By deriving a new interface from an existing interface, you define a new and specialized contract, and any class that implements that interface must implement all members of the base interface(s). A class can choose to implement multiple interfaces, just as a person can choose to commit to multiple contracts.


If you want to add some thing, please drop your comments



2 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