Are defaults in JDK 8 a form of multiple inheritance in Java?

前端 未结 8 1744
北荒
北荒 2020-11-30 00:17

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 S         


        
相关标签:
8条回答
  • 2020-11-30 00:41

    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.

    0 讨论(0)
  • 2020-11-30 00:44

    There are two scenarios:

    1) First, that was mentioned, where there is no most specific 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) Second, when there 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 讨论(0)
  • 2020-11-30 00:52

    "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:

    interface A{
    default public void m(){
    System.out.println("Interface A: m()");
    }
    }
    
    interface B{
    default public void m(){
    System.out.println("Interface B: m()");
    }
    }
    
     class C implements A,B { 
    
     public void m(){
      System.out.println("Concrete C: m()");   
     }
    
    public static void main(String[] args) {
       C aC = new C();
       aC.m();
       new A(){}.m();
       new B(){}.m();
    }
    }
    

    Class C above must implement its own concrete method of the interfaces A and B. Namely:

     public void m(){
      System.out.println("Interface C: m()");   
     }
    

    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:

    new A(){}.m();
    

    The output of the above would be:

    Interface A: m()

    0 讨论(0)
  • 2020-11-30 00:58

    As far as I see it, it is no multiple inheritance because they are stateless. So virtual extension methods don't support full object or class functionality.

    0 讨论(0)
  • 2020-11-30 01:02

    If anyone's still looking for an answer, in case a class implements two interfaces with the same default method then the class needs to resolve the disambiguity by providing an implementation of its own. Look at this tutorial for more details on how inheritance in default methods work.

    0 讨论(0)
  • 2020-11-30 01:03

    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:

     class TimeTravelingStudent inherits unrelated defaults for present() from types Attendance and Timeline reference to present is ambiguous, both method present() in Timeline and method present() in Attendance match.

    0 讨论(0)
提交回复
热议问题