A new feature coming in JDK 8 allows you to add to an existing interface while preserving binary compatibility.
The syntax is like
public interface SomeInterface() { void existingInterface(); void newInterface() default SomeClass.defaultImplementation; }
This way for all existing implementations of SomeInterface
when they upgrade to this new version they don't all suddenly have compiles errors around newInterface()
.
While this is neat, what happens when you are implementing two interfaces which both have added a new defaulted method which you did not implement? Let me explain with an example.
public interface Attendance { boolean present() default DefaultAttendance.present; } public interface Timeline { boolean present() default DefaultTimeline.present; } public class TimeTravelingStudent implements Attendance, Timeline { } // which code gets called? new TimeTravelingStudent().present();
Has this been defined as part of JDK 8 yet?
I found the Java gods talking about something similar here http://cs.oswego.edu/pipermail/lambda-lib/2011-February/000068.html, but its part of private mailing list and I cannot ask them directly.
See this for more details on how defaults are going to be used in JDK 8 and extending the Collection interface to support lambdas: https://oracleus.wingateweb.com/published/oracleus2011/sessions/25066/25066_Cho223662.pdf
For interfaces, yes. If we are writing 2 different interface having same default method but with different return type then Java expects the return type to be co-variant type in the implementing class.
Default methods and abstract methods in interfaces are inherited like instance methods. However, when the supertypes of a class or interface provide multiple default methods with the same signature, the Java compiler follows inheritance rules to resolve the name conflict.
Java 8 introduces default method so that List/Collection interface can have a default implementation of forEach method, and the class implementing these interfaces need not implement the same.
Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code.
The answer to the duplicate operation is:
To solve multiple inheritance issue a class implementing two interfaces providing a default implementation for the same method name and signature must provide an implementation of the method. [Full Article]
My answer to your question is: Yes, it is a form of multiple inheritance, because you can inherit behavior from different parents. What's missing is to inherit states, i. e., attributes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With