Thursday, March 11, 2010

Difference between Interface and Abstract Class

When To Use Interfaces

----------------------------------------------------------------------------------
An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package.


When To Use Abstract classes

----------------------------------------------------------------------------------
An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.


When to Use Both

---------------------------------------------------------------------------------
You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.

Interfaces vs Abstract Classes
------------------------------

1> :multiple inheritance-interface: A class may implement several interfaces.
abstract class :A class may extend only one abstract class.
=======================================================================
***********************************************************************
=======================================================================
2 :default implementation-interface: An interface cannot provide any code at all, much less default code.
abstract class: An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.
=======================================================================
***********************************************************************
=======================================================================
3 :constants-interface: Static final constants only, can use them without qualification in classes that implement the interface. On the other paw, these unqualified names pollute the namespace. You can use them and it is not obvious where they are coming from since the qualification is optional.
abstract class: Both instance and static constants are possible. Both static and instance intialiser code are also possible to compute the constants.
========================================================================
***********************************************************************
========================================================================
4 :third party convenience-interface: An interface implementation may be added to any existing third party class.
abstract class: A third party class must be rewritten to extend only from the abstract class.
=======================================================================
***********************************************************************
=======================================================================
5 :plug-in-interface: You can write a new replacement module for an interface that contains not one stick of code in common with the existing implementations. When you implement the interface, you start from scratch without any default implementation. You have to obtain your tools from other classes; nothing comes with the interface other than a few constants. This gives you freedom to implement a radically different internal design.
abstract class: You must use the abstract class as-is for the code base, with all its attendant baggage, good or bad. The abstract class author has imposed structure on you. Depending on the cleverness of the author of the abstract class, this may be good or bad.
==========================================================================
************************************************************************
=========================================================================
6 :adding functionality-interface: If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.
abstract class: If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.

______________________________________________________________________________
*********************************************************************************** ================ Important =================

No comments:

Post a Comment