官术网_书友最值得收藏!

What if a class implements two interfaces which have default methods with same name and signature?

Interfaces in Java are used to provide multiple inheritances. Classes can implement more than one interface. As interface can consist of default methods in Java 8, which a class does not necessarily need to implement. We can have two interfaces that have default methods with the same name and signature.

Let's create two interfaces with the same default method:

public interface Interface1 { 
   default void hello(){ 
      System.out.println("Hello from Interface1"); 
   } 
} 
 
public interface Interface2 { 
   default void hello(){ 
      System.out.println("Hello from Interface1"); 
   } 
} 

So, we have two interfaces with the default method hello with the same signature:

public class InterfaceImpl implements Interface1,Interface2{ 
   @Override 
   public void hello() { 
      Interface1.super.hello(); 
   } 
} 

Now, once you create a class that implements both of these interfaces, the compiler will force you to override the hello method. You can call hello of any of the interfaces if you want using the syntax given previously or you can given a different implementation to it.

主站蜘蛛池模板: 天水市| 新干县| 黑河市| 新郑市| 沂源县| 平潭县| 长泰县| 庆安县| 搜索| 望谟县| 舞阳县| 图片| 那坡县| 清水河县| 镇安县| 蕲春县| 内乡县| 宜宾市| 延吉市| 古浪县| 商都县| 岳阳县| 维西| 西藏| 无锡市| 渝中区| 托克托县| 修水县| 鱼台县| 普格县| 泸水县| 建水县| 湘西| 饶河县| 彰武县| 永和县| 中西区| 屏边| 安多县| 潍坊市| 博野县|