Are JDK 8 default values ​​a form of multiple inheritance in Java?

A new feature included in JDK 8 allows you to add to an existing interface while maintaining binary compatibility.

The syntax is similar to

public interface SomeInterface() { void existingInterface(); void newInterface() default SomeClass.defaultImplementation; } 

Thus, for all existing SomeInterface implementations SomeInterface when upgrading to this new version, not all of them suddenly compile errors around newInterface() .

While this is neat, what happens when you implement two interfaces that both added a new default method that 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(); 

Was this defined as part of JDK 8?

I found Java gods talking about something similar here http://cs.oswego.edu/pipermail/lambda-lib/2011-February/000068.html , but its part of a personal mailing list, and I cannot directly request them .

For more information about the default usage in JDK 8 and the extension of the Collection interface to support lambdas, see this section. https://oracleus.wingateweb.com/published/oracleus2011/sessions/25066/25066_Cho223662.pdf

+69
java closures java-8 interface multiple-inheritance
Oct 22 '11 at 6:25
source share
8 answers

The video session for viewing is here http://medianetwork.oracle.com/video/player/1113272518001 This is a designer talking about a feature called Virtual Extensions. He also talks about how this does not violate compatibility.

+7
Oct 22 '11 at 7:35
source share

Response to duplicate operation:

To solve the problem of multiple inheritance, a class that implements two interfaces that provide a default implementation for the same method name and signature must provide the implementation of the method. [Full article]

My answer to your question: Yes, this is a form of multiple inheritance, because you can inherit behavior from different parents. What is missing for state inheritance, i.e. e., attributes.

+59
Oct 22
source share

I know this is an old post, but since I work with this material ...

You will have a compiler error telling you that:

οΏΌ the TimeTravelingStudent class inherits unrelated default values ​​for present () from the Attendance and Timeline types; the reference to the present is ambiguous, both methods are present () in Timeline and the present () method in the Attendance match.

+5
Aug 15 '13 at 4:28
source share

My answer to your question: Yes, this is a form of multiple inheritance, because you can inherit behavior from different parents. What is missing for state inheritance, i.e. e., attributes.

Yes, but you can add getters and setters to your interface, which must implement implementing classes. However, implementation classes do not inherit attributes. Thus, AFAICS, it is more like a style-in-style solution, rather than a multiple inheritance solution.

+2
Jul 26 2018-12-12T00:
source share

If someone is still looking for an answer, if the class implements two interfaces with the same default method, the class must resolve the ambiguity by providing its own implementation. Check out this tutorial for more details on how inheritance works in default methods.

+1
Aug 04 '14 at 6:46
source share

In short: this is a compile-time error that must be performed on a method manually in an implementation.




Default Method Assignment

The main goal of implementing the default method in Java 8 is to make the interface extensible without breaking existing implementations (there are so many third-party Java libraries).

And multiple inheritance , as in C ++, is actually supposed to be avoided, which is definitely not the goal of the default method in Java.




How to override

2 options:

  • Override the method with its own logic.
  • Override the method, call one of the interface methods via super , format: <interface_name>.super.<method_name>();

Tips:

  • the method from the interface is public by default, so be sure to add the public keyword if it is overridden.
+1
Jul 19 '16 at 5:08
source share

There are two scenarios:

1) Firstly, it was mentioned where it does not have a special interface

 public interface A { default void doStuff(){ /* implementation */ } } public interface B { default void doStuff() { /* implementation */ } } public class C implements A, B { // option 1: own implementation // OR // option 2: use new syntax to call specific interface or face compilation error void doStuff(){ B.super.doStuff(); } } 

2) Secondly, when IS is a more specific interface:

  public interface A { default void doStuff() { /* implementation */ } } public interface B extends A { default void doStuff() { /* implementation */ } } public class C implements A, B { // will use method from B, as it is "closer" to C } 
0
May 11 '17 at 19:34
source share

As far as I can see, this is not multiple inheritance, because they are stateless. Thus, virtual extension methods do not support the full functionality of objects or classes.

-one
Jan 6 '14 at 16:15
source share



All Articles