default method - amresh087/newronaRepos GitHub Wiki

In interface default method is one problem during multiple inheritance because suppose a class implements multiple interface but both interface contain same method that time compiler get ambiguity problem.

            interface Left
             { 
                default  void m1()
                 {
                   System.out.println("from m1 of left");
                 }
              }

            interface Right
             { 
                default  void m1()
                 {
                   System.out.println("from m1 of right");
                 }
              }


           class Test implements Left,Right
            {
            }

Now if you compile Test class that time you got compile time error Duplicate default methods named m1 with the parameters () and () are inherited from the types Right and Left

Solution of these problem

you can solved this problem by overriding m1() in Test class otherwise if you want Left interface default then Test class override m1() in m1 method you call Left.super.m1();

				package com.amresh.functional;
				
				interface Left {
					default void m1() {
						System.out.println("from m1 of left");
					}
				}
				
				interface Right {
					default void m1() {
						System.out.println("from m1 of right");
					}
				}
				
				class Test implements Left, Right {
				
					public void m1() {
				
						System.out.println("from own m1 method");
				
						Left.super.m1();
				
					}
				}
				
				public class DefaultDemo {
				
					public static void main(String[] args) {
				
						Test t = new Test();
				
						t.m1();
				
					}
				
				}