As Java 8 allows for default implementation of methods in interface called Default Methods, there seems to be a confusion between when would I use a abstract class
.
So when should interface with default methods be used and when should an abstract class be used? Are the abstract classes still useful in that scenario?
Regarding your query of
java documentation provides perfect answer.
Abstract Classes Compared to Interfaces:
Use cases for each of them have been explained in below SE post:
What is the difference between an interface and abstract class?
Yes. They are still useful. They can contain non-static, non-final methods and attributes (protected, private in addition to public), which is not possible even with Java-8 interfaces.
Please think first of open/closed principle. The default methods in interfaces DO VIOLATE it. This is a bad feature in Java. It encourages bad design, bad architecture, low software quality. I would suggest to avoid using default methods completely.
Ask yourself a few questions: Why can't you put your methods to the abstract class? Would you need then more than one abstract class? Then think about what is your class responsible for. Are you sure all methods you are going to put to the single class really fulfill the same purpose? May be you will distinguish several purposes and will then split your class into several classes, for each purpose its own class.
As described in this article,
Abstract classes versus interfaces in Java 8
Whenever we have a choice between abstract class and interface we should always (almost) prefer default (also known as defender or virtual extensions) methods.
Collection and AbstractCollection
. Now we should implement the methods in the interface itself to provide default functionality. The classes which implement the interface has choice to override the methods or inherit the default implementation.Another important use of default methods is
interface evolution
. Suppose I had a class Ball as:public class Ball implements Collection { ... }
Now in Java 8 a new feature streams in introduced. We can get a stream by using
stream
method added to the interface. Ifstream
were not a default method all the implementations forCollection
interface would have broken as they would not be implementing this new method. Adding a non-default method to an interface is notsource-compatible
.But suppose we do not recompile the class and use an old jar file which contains this class
Ball
. The class will load fine without this missing method, instances can be created and it seems everything is working fine. BUT if program invokesstream
method on instance ofBall
we will getAbstractMethodError
. So making method default solved both the problems.Default methods in Java Interface are to be used more for providing dummy implementation of a function thus saving any implementing class of that interface from the pain of declaring all the abstract methods even if they want to deal with only one. Default methods in interface are thus in a way more a replacement for the concept of adapter classes.
The methods in abstract class are however supposed to give a meaningful implementation which any child class should override only if needed to override a common functionality.
Backward compatibility: Imagine that your interface is implemented by hundreds of classes, modifying that interface will force all the users to implement the newly added method, even though it could be not essential for many other classes that implements your interface, Plus it allows your interface to be a functional interface
Facts & Restrictions:
1-May only be declared within an interface and not within a class or abstract class.
2-Must provide a body
3-It is not assumed to be abstract as other normal methods used in an interface.