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
Yes, but you can add getters and setters to your interface that the implementing classes then must implement. Nevertheless, the implementing classes don't inherit attributes. So, AFAICS, it's more like a trait-style solution rather than a multiple inheritance style solution.
The video session to watch is here http://medianetwork.oracle.com/video/player/1113272518001 This is the designer talking about the feature called Virtual Extensions. He also talks about how this doesn't break backward compatibility.
I know this is a old post, but as i'm working with this stuff...
You will have an error from the compiler, telling you that:
In short: it's a compile time error, must override the method by hand in the implementation.
Purpose of default method
The major purpose to introduce default method in Java 8, is to make interface extendable, without breaking existing implementations (there are so many 3rd party Java libraries).
And
multiple inheritance
like in C++ is actually intended to be avoided, that's definitely not the purpose of default method in Java.How to override
2 options:
super
, format:<interface_name>.super.<method_name>();
Tips:
public
keyword when override it.The answer to the duplicate operation is:
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.
"How will we distinguish the methods" was a question that was put on Stackoverflow and referred to this question concrete methods in interfaces Java1.8
The following is an example that should answer that question:
Class C above must implement its own concrete method of the interfaces A and B. Namely:
To call a concrete implementation of a method from a specific interface, you can instantiate the interface and explicitly call the concrete method of that interface
For example, the following code calls the concrete implementation of the method m() from interface A:
The output of the above would be:
Interface A: m()