- 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.
推薦閱讀
- Visual C++程序設計學習筆記
- 深入理解Bootstrap
- PyTorch自動駕駛視覺感知算法實戰
- Mastering Objectoriented Python
- GeoServer Beginner's Guide(Second Edition)
- 程序員修煉之道:通向務實的最高境界(第2版)
- Haxe Game Development Essentials
- UML 基礎與 Rose 建模案例(第3版)
- C語言程序設計上機指導與習題解答(第2版)
- Java網絡編程核心技術詳解(視頻微課版)
- Mastering Git
- Java程序員面試筆試寶典(第2版)
- JavaScript動態網頁編程
- Scala Functional Programming Patterns
- HTML5 WebSocket權威指南