Wednesday, November 5, 2014

Interface vs Abstract class in Java 8

Difference between Interface and Abstract class in Common

In Java you can extend only one class (because the Diamond of Death), whether or not it is abstract, whereas you can implement any number of interfaces.

In interfaces, all fields are public, static, and final, and all declared methods are public.
In abstract classes, you can declare concrete fields and methods with any existing access modifiers. 
So, an Interface is about public API.
The contract that has to be followed during the implementation.
It means that all implementation objects are guaranteed to provide the functionality declared in interface.
But an Abstract class is about Common implementation.
The basic design that has to be expanded or reused.
It means that all objects that extend an abstract class have to implement
their specifics(missing pieces
) to became some kind of concrete entity.
Based on above, we also can say, that Interfaces supposed to combine not related entities with similar abilities, whereas Abstract classes supposed to share common stuff among the several kinds of the same entity.


Difference between Interface and Abstract class in Java 8


Differentiating between the two is even more difficult in JDK 8, cause it provides an ability to define default and static methods in interfaces (Oracle Doc. JDK 8 Default methods)

An advantage is that you are able to provide a default implementation for the third parties instead of forcing them to implement all by their own.
It means that  any class that implements your interface, might have already defined
 methods for doing cool stuff by default.
It also means that  interfaces with default methods can likely be used instead of large number of abstract classes work.

Difference has become more smooth, but semantically it stays the same as above: It's possible to extend only one class (because the Diamond of Death),
whether or not it is abstract, whereas you can implement any number of interfaces.

You may ask "What will happen if class implements two interfaces, 
both of which describe a default method with the same signature?"
The answer is "Compilation error, that 
interface inherits unrelated defaults and you have to specify which one you want to use" - gotcha:)


Interface default method vs. Abstract class When to use?


Interface default method might be considered advantageous when a particular implementation needs to be associated with multiple types with no reference to
an appropriate implementation state. 


So, use case are high-level convenient methods.


Interface
default methods brings arguably greatest advantage of abstract classes,
which means that you can have a really clean design and minimize implementation effort
of programmer to not force him to use an abstract classes,
where 
only some convenient methods are required.

Another-words, default methods allows you to follow components based approach,
likely to use composition over inheritance.

You can find some good examples in java library, where we have:

list.sort(comparator);
when previously
Collections.sort(list, comparator);

Whereas, you still have to use an Abstract class if you need to share some code among the several superclasses, that expect to have common fields/methods, which can have private/protected modifiers or belong to your object state.



No comments:

Post a Comment