Organizational Research By

Surprising Reserch Topic

how to use abstract class vs interface in java

abstract class vs interface in java  using -'java,design-patterns'

I was asked a question, I wanted to get my answer reviewed here.

Q: In which scenario it is more appropriate to extend a abstract class rather than implementing the interface(s)?

A: If we are using template method design pattern.

Am I correct ?

I am sorry if I was not able to state the question clearly.
I know the basic difference between abstract class and interface.
1) use abstract class when the requirement is such that we need to implement the same functionality in every subclass for a specific operation (implement the method) and different functionality for some other operations (only method signatures)
2) use interface if you need to put the signature to be same (and implementation different) so that you can comply with interface implementation
3) we can extend max of one abstract class, but can implement more than one interface  

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)

Edit on Aug 2013
One valid point I got as answer is

Interface vs. Abstract class

Choosing between these two really depends on what you want to do, but luckily for us, Erich Gamma can help us a bit.

As always there is a trade-off, an interface gives you freedom with regard to the base class, an abstract class gives you the freedom to add new methods later. – Erich Gamma

You can’t go and change an Interface without having to change a lot of other things in your code, so the only way to avoid this would be to create a whole new Interface, which might not always be a good thing.

Abstract classes should primarily be used for objects that are closely related
Interfaces are better at providing common functionality for unrelated classes.

asked Sep 9, 2015 by MarilynnKels
0 votes

Related Hot Questions

8 Answers

0 votes

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.

answered Sep 9, 2015 by ReedBodnarlt
0 votes

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:


    A class can implement multiple interfaces
    An interface cannot provide any code at all
    An interface can only define public static final constants
    An interface cannot define instance variables
    Adding a new method has ripple effects on implementing classes (design maintenance)
    JAXB cannot deal with interfaces
    An interface cannot extends or implement an abstract class
    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

    A class can extend at most one abstract class
    An abstract class can contain code
    An abstract class can define both static and instance constants (final)
    An abstract class can define instance variables
    Modification of existing abstract class code has ripple effects on extending classes (implementation maintenance)
    Adding a new method to an abstract class has no ripple effect on extending classes
    An abstract class can implement an interface
    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.
answered Sep 9, 2015 by LesliLeveret
0 votes

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 :

answered Sep 9, 2015 by FelicitasCha
0 votes

In my opinion , the basic difference is that an interface can't contain non abstract methods while an abstract class can. So if subclasses share a common behaviour, this behaviour can be implemented in the super class and thus inherited in the subclasses

Also I quoted the following from "software architecture design ppatterns in java" book

" In the Java programming language there is no support for multiple inheritance. That means a class can inherit only from one single class. Hence inheritance should be used only when it is absolutely necessary. Whenever possible, methods denoting the common behavior should be declared in the form of a Java interface to be implemented by different implementer classes. But interfaces suffer from the limitation that they cannot provide method implementations. This means that every implementer of an interface must explicitly implement all methods declared in an interface, even when some of these methods represent the invariable part of the functionality and have exactly the same implementation in all of the implementer classes. This leads to redundant code. The following example demonstrates how the Abstract Parent Class pattern can be used in such cases without requiring redundant method implementations."

answered Sep 9, 2015 by JamesPardey
0 votes

The shortest answer is, extend abstract class when some of the functionalities uou seek are already implemented in it.

If you implement the interface you have to implement all the method. But for abstract class number of methods you need to implement might be fewer.

In template design pattern there must be a behavior defined. This behavior depends on other methods which are abstract. By making sub class and defining those methods you actually define the main behavior. The underlying behavior can not be in a interface as interface does not define anything, it just declares. So a template design pattern always comes with an abstract class. If you want to keep the flow of the behavior intact you must extend the abstract class but don't override the main behavior.

answered Sep 9, 2015 by EleanorFoti
0 votes

Abstract classes are different from interfaces in two important aspects

  • they provide default implementation for chosen methods (that is covered by your answer)
  • abstract classes can have state (instance variables) - so this is one more situation you want to use them in place of interfaces
answered Sep 9, 2015 by EleanorFoti
0 votes

Abstract classes should be extended when you want to some common behavior to get extended. The Abstract super class will have the common behavior and will define abstract method/specific behavior which sub classes should implement.

Interfaces allows you to change the implementation anytime allowing the interface to be intact.

answered Sep 9, 2015 by RegGottshal
0 votes

Which should you use, abstract classes or interfaces?

Consider using abstract classes if any of these statements apply to your situation:

You want to share code among several closely related classes.

You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).

You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

Consider using interfaces if any of these statements apply to your situation:

You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.

You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.

You want to take advantage of multiple inheritance of type.

answered Sep 9, 2015 by SherleneNeed