When to go for Interfaces and Abstract Classes?

From Java How to Program about abstract classes:

Because they’re used only as superclasses in inheritance hierarchies, we refer to them as abstract superclasses. These classes cannot be used to instantiate objects, because abstract classes are incomplete. Subclasses must declare the “missing pieces” to become “concrete” classes, from which you can instantiate objects. Otherwise, these subclasses, too, will be abstract.
To answer your question "What is the reason to use interfacs?":

An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design.
As opposed to an interface:

An interface describes a set of methods that can be called on an object, but does not provide concrete implementations for all the methods... Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality described by the interface. This is true of all subclasses of that class as well.
So, to answer your question "I was wondering when I should use interfaces", I think you should use interfaces when you want a full implementation and use abstract classes when you want partial pieces for your design (for reusability)


From the Oracle tutorials :

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.

By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).


Abstract Classes by serving as a base class, gives its subclass "IS-A" Relationship.
Interfaces on the other hand gives a badge that says "Can Do"  for the classes that implement them .

Note:
        Go for Abstract Classes if your logic is going to change frequently in your project's lifecycle.
        Because if Interfaces were used in that scenario, your code will get messed up.

Other Userful References
http://tutorials.jenkov.com/java/interfaces-vs-abstract-classes.html
http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/
http://www.quora.com/In-Java-when-should-you-use-an-interface-instead-of-an-abstract-class

Comments

Popular posts from this blog

UML,HLD and LLD.

How SQL Indexes Work internally ?

XML Schema (XSD)