- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 174字
- 2021-07-02 19:01:56
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.
推薦閱讀
- Mastering OpenLayers 3
- Android Wearable Programming
- 自然語(yǔ)言處理實(shí)戰(zhàn):預(yù)訓(xùn)練模型應(yīng)用及其產(chǎn)品化
- 小程序?qū)崙?zhàn)視頻課:微信小程序開發(fā)全案精講
- INSTANT FreeMarker Starter
- 軟件測(cè)試工程師面試秘籍
- VMware vSphere 6.7虛擬化架構(gòu)實(shí)戰(zhàn)指南
- Serverless架構(gòu)
- VMware虛擬化技術(shù)
- Kotlin編程實(shí)戰(zhàn):創(chuàng)建優(yōu)雅、富于表現(xiàn)力和高性能的JVM與Android應(yīng)用程序
- 從零開始學(xué)Linux編程
- 時(shí)空數(shù)據(jù)建模及其應(yīng)用
- Managing Microsoft Hybrid Clouds
- 新手學(xué)ASP.NET 3.5網(wǎng)絡(luò)開發(fā)
- Appcelerator Titanium Smartphone App Development Cookbook