Default Methods in Java 8 and Multiple Inheritance

From Wiki,

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.

Default methods in Java 8 can be viewed as a form of multiple inheritance (except that attribute can not be inherited). Consider the example below, the Button class implements two interfaces - Clickable and Accessible.

default-methods-java-8

Each interface defines a default method. The Button class therefore can call methods from both interfaces. This is like a multiple inheritance.

interface Clickable{
	default void click(){
		System.out.println("click");
	}
}
 
interface Accessible{
	default void access(){
		System.out.println("access");
	}
}
 
public class Button implements Clickable, Accessible {
 
	public static void main(String[] args) {
		Button button = new Button();
		button.click();
		button.access();
	}
}

If both of the implemented interfaces define a default method with same method signature, then the implementation class does not know which default method to use. The implementation class should define explicitly specify which default method to use or define it's own one. In the example below, Clickable and Accessible both define the print() method. In the Button class, the print() method specifies the default method.

default-methods-java-8-same-name

interface Clickable{
	default void click(){
		System.out.println("click");
	}
 
	default void print(){
		System.out.println("Clickable");
	}
}
 
interface Accessible{
	default void access(){
		System.out.println("access");
	}
 
	default void print(){
		System.out.println("Accessible");
	}
}
 
public class Button implements Clickable, Accessible {
 
	public void print(){
		Clickable.super.print();
		Accessible.super.print();
	}
 
	public static void main(String[] args) {
		Button button = new Button();
		button.click();
		button.access();
		button.print();
	}
}

The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation classes. In this way, the interface is still compatible with older versions. This is a cool feature. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.

To know more features of Java 8, check out Simple Java 8.

Category >> Java 8  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Ulf Peter Langenbach

    The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation classes.

    Alas, this doesn’t work. With code within interfaces, we do run into the diamond problem right away:

    1. Create interfaces Accessible and Clickable and class Button like in the first example
    2. Add default void print() {...} to Clickable
    3. Add button.print(); to main() (like in the second example)
    4. Try it – it runs fine, as expected
    5. Add default void print() {...} to Accessible
    6. VoilĂ  – compile time error in class Button

  • kumar venkatesan

    Please accept my request. permission denied coming

  • JAC_JustAnotherCoder

    oracle is dying to copy everything from scala to java , to keep java breathing.

    Poor oracle

  • Simon Cantor

    That’s horrible. It contradicts to definition of interface as OOD (and OOP) phenomena, wipes out I in SOLID and makes much other such “advances “

  • infoj

    There are 3 scenarios that may arise because of same default method in 2 interfaces. read more about it here – http://netjs.blogspot.in/2015/05/interface-default-methods-in-java-8.html

  • jsdevel

    Thanks for sharing the information! Default methods really made Selenium PageObjects a whole lot easier. See this post for examples: http://blog.jsdevel.me/2015/04/pageobjects-done-right-in-java-8.html

  • thanks for sharing this post… its really awesome
    psd templates for websites