---
# System prepended metadata

title: Diamond problem doesn't exist due to default methods in interfaces. As compiler enforces you to implement the default method in child class.

---

# Diamond problem doesn't exist due to default methods in interfaces. As compiler enforces you to implement the default method in child class.

###### tags: java, diamond problem

consider the below code 

**Example 1:**

```:java 
interface MyInterface{
  public default void display(){
    System.out.println("display method of MyInterface");
  }
}

interface MyInterface1 extends MyInterface{
  public   int num = 100;
  public default void display() {
    System.out.println("display method of MyInterface1");
  }
}
interface MyInterface2 extends MyInterface{
  public static int num = 1000;
  public default void display() {
    System.out.println("display method of MyInterface2");
  }
}
class InterfaceExample implements MyInterface1, MyInterface2{
  public void display() {
    MyInterface1.super.display();
    //or,
    MyInterface2.super.display();
    System.out.println("dsf");
  }

}
public  class Test6{

  public static void main(String args[]) {
    InterfaceExample obj = new InterfaceExample();
    obj.display();
  }
}
```

InterfaceExample class implements both MyInterface1 and MyInterface2 classes, both have default methods, so if we don't implement the display() method here, which one will be called during obj.display(). 

None, the compiler basically forces you to implement the default display method to avoid this. 

> Interface cannot have instance variables in it's class, it can only have constants. 

> Abstract classes are helpful over Interfaces because of this, that they can have instance variables as part of their class body. 


**Example 2:** 

```java 
class BaseClass {
    public void foo() { System.out.println("BaseClass's foo"); }
}
interface BaseInterface {
    default public void foo() { System.out.println("BaseInterface's foo"); }
}
public class Diamond extends BaseClass implements BaseInterface {
    public static void main(String []args) {
        new Diamond().foo();
    }
}
```

in this case the for `Diamond().foo()` the `BaseClass.foo()` method will get executed because of the `Class wins` rule. 

> if both interface and other class have a method with same signature then class's method will be given priority
