I know that there is already a question related to this, Interface with default methods vs Abstract class in Java 8 but that question was asked way before Java 8 was released and it is just a gist of what is the difference between Abstract classes and interfaces with default methods, and it doesn't provide any practical examples at all, rather it is just full of wordily explanation.
I am posting this question because I am more interested in a practical example that clears the difference between an Abstract Class and Interface with a default method in Java 8.
Have a look at this example,
Abstract Class,
abstract class AbstractClass {
public int var = 0;
// These methods must be Overridden
abstract int x();
abstract int y();
// This method is optional
int z() {
return 0;
}
}
Interface (Java 8)
interface Interface {
public int var = 0;
// These methods must be Overridden
int x();
int y();
// This method is optional
default int z(){
return 0;
}
}
How could I differentiate in above two if I want to explain its practical difference to a newbie Java programmer (like myself) ?
this article says,
The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the implementations.
This couldn't possibly be the only benefit of interfaces with default methods, or is it ?