Abstract class vs Interface in Java Abstract class vs Interface in Java java java

Abstract class vs Interface in Java


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. The disadvantage is every method in the interface must be public. You might not want to expose everything.

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.


reiterating the question: there is any other scenario besides these mentioned above where specifically we require to use abstract class (one is see is template method design pattern is conceptually based on this only)

Yes, if you use JAXB. It does not like interfaces. You should either use abstract classes or work around this limitation with generics.

From a personal blog post:

Interface:

  1. A class can implement multiple interfaces
  2. An interface cannot provide any code at all
  3. An interface can only define public static final constants
  4. An interface cannot define instance variables
  5. Adding a new method has ripple effects on implementing classes (design maintenance)
  6. JAXB cannot deal with interfaces
  7. An interface cannot extends or implement an abstract class
  8. All interface methods are public

In general, interfaces should be used to define contracts (what is to be achieved, not how to achieve it).

Abstract Class:

  1. A class can extend at most one abstract class
  2. An abstract class can contain code
  3. An abstract class can define both static and instance constants (final)
  4. An abstract class can define instance variables
  5. Modification of existing abstract class code has ripple effects on extending classes (implementation maintenance)
  6. Adding a new method to an abstract class has no ripple effect on extending classes
  7. An abstract class can implement an interface
  8. Abstract classes can implement private and protected methods

Abstract classes should be used for (partial) implementation. They can be a mean to restrain the way API contracts should be implemented.


Interface is used when you have scenario that all classes has same structure but totally have different functionality.

Abstract class is used when you have scenario that all classes has same structure but some same and some different functionality.

Take a look the article : http://shoaibmk.blogspot.com/2011/09/abstract-class-is-class-which-cannot-be.html