12
Java 8 allows you to implement methods in the interface itself.
So I’d like to know what an abstract class can do that an interface cannot.
Source: http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/
12
Java 8 allows you to implement methods in the interface itself.
So I’d like to know what an abstract class can do that an interface cannot.
Source: http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/
11
Abstract classes can contain state and interface cannot. This is the main justification for using it from the more technical point of view. Obviously because it has been, it can have constructs, interface not.
Another reason is the possibility of having private members. Interfaces can still only have public methods, at least for now, it makes no sense to maintain this restriction.
From the conceptual point of view the abstract class still passes the idea that the derived object is a base object. The interface continues to convey the idea that the object using the interface only has a specific behaviour.
Remembering that it has always been more interesting, in most scenarios, to create interfaces than abstract classes to the extent possible.
That is, it does not change anything, unless it has slightly automated the process of using interface. You will not use interfaces where you need to use an abstract class, unless you use an abstract class where you shouldn’t. Improved use of the interface, did not change the use case of each of the mechanisms.
Until Java 7 the concrete method that fulfills the interface contract needed to be written in the class. There were cases that this could generate duplication of code. To avoid duplication the solution was to create a utility class (calling for Companion class) with concrete implementation. There inside the method in the concrete class that implements the interface was just calling the utility method of this companion class.
In Java 8, you don’t have to do this. The method is already written inside the interface and in the concrete class does not need to write anything, the class already knows what to run. Obviously the utility class is no longer needed. Facilitates code reuse and improves encapsulation.
Note that nothing prevents you from implementing something different and overriding the implementation default of the interface. And obviously it is required to implement the concrete method, creating a disambiguation, if there is more than one interface requiring the same method (same signature)
Excellent, very good explanation.
Great explanation.
Browser other questions tagged java oop java-8 interface abstract-classes
You are not signed in. Login or sign up in order to post.
In that question there is a reply about java8 however it was erased by the author, maybe it would be interesting to resurrect it. If you can not see, has a image here
– rray
Implement the methods in the interface? You can pass the reference of this?
– Jéf Bueno
@jbueno of course, I was also amazed when the bigown informed me, and that was the motivation to create the question and make it clearer among the community.
– David Schrammel