Last Modified 2019.3.4

Default Method

The default method allows you to define an interface that contains a method with an implementation. The introduction of the default method was an inevitable choice to change the API while maintaining compatibility with legacy code. This makes Java a language that allows multiple inheritance. To declare a default method, prefix the return type with default.

Suppose that C inherits A and B. If A and B have the same method, which method does C inherit? This is determined by the following rules:

  1. The class always wins. The method defined in the class or superclass wins the default method.
  2. In a situation other than rule #1, the sub interface wins.
  3. If Java still does not determine the priority of the default method, you should explicitly override and call the default method.

1. The class always wins.

package net.java_school.examples;

public interface A1Interface {
	public default String hello() {
		return "A1 Interface says hello";
	}
}
package net.java_school.examples;

public class B1Class {
	public String hello() {
		return "B1 Class says hello";
	}
}
package net.java_school.examples;

public class C1Class extends B1Class implements A1Interface {

	public static void main(String[] args) {
		C1Class c1 = new C1Class();
		System.out.println(c1.hello());
	}

}
B1 Class says hello

2. Sub interface wins

package net.java_school.examples;

public interface A2Interface {
	public default String hello() {
		return "A2 Interface says hello";
	}
}
package net.java_school.examples;

public interface B2Interface extends A2Interface {
	@Override
	public default String hello() {
		return "B2 Interface says hello";
	}
}
package net.java_school.examples;

public class C2Class implements A2Interface,B2Interface {
	public static void main(String[] args) {
		C2Class c2 = new C2Class();
		System.out.println(c2.hello());
	}
}
B2 Interface says hello

3. Sometimes, you should explicitly override and call the default method.

package net.java_school.examples;

public interface A3Interface {
	public default String hello() {
		return "A3 Interface says hello";
	}
}
package net.java_school.examples;

public interface B3Interface {
	public default String hello() {
		return "B3 Interface says hello";
	}
}
package net.java_school.examples;

public class C3Class implements A3Interface,B3Interface {

}

In the C3Class class declaration, a compilation error occurs that starts with Duplicate default methods named hello .... To avoid compile errors, you must explicitly override the method you choose.

package net.java_school.examples;

public class C3Class implements A3Interface,B3Interface {

	@Override
	public String hello() {
		return B3Interface.super.hello();//B3Interface's hello().
	}

	public static void main(String[] args) {
		C3Class c3 = new C3Class();
		System.out.println(c3.hello());
	}

}
B3 Interface says hello

Final source : https://github.com/kimjonghoon/multipleInheritance

How to run

~/multipleInheritance$ cd src/net/java_school/examples/
~/multipleInheritance/src/net/java_school/examples$ javac -d ../../../../bin *.java
~/multipleInheritance/src/net/java_school/examples$ cd -
~/multipleInheritance$ java -cp ./bin net.java_school.examples.Test
B1 Class says hello
B2 Interface says hello
B3 Interface says hello