Java 8 Default Methods Explained in 5 minutes
In my previous articles, we have looked at Java 8 Lambda Expressions and Streams. In this article will be looking at Defaults Methods which is another cool feature of Java 8.
Default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface. Lets take a look at the example below.
public class MyClass implements InterfaceA { * @param args the command line arguments public static void main(String[] args) { // TODO code application logic here @Override public void saySomething() { System.out.println("Hello World"); interface InterfaceA { public void saySomething(); }
The code above shows class MyClass implementing InterfaceA’s method saySomething(). Now lets add a new method called sayHi() to InterfaceA. By doing so, we have introduce a problem to class MyClass as it will not compile until we provide implementation for method sayHi().
This is when Defaults methods becomes useful. By Adding the keyword default before the method’s access modifier, we do not have to provide implementation for the method sayHi() in class MyClass.
In ‘the strictest sense’, Default methods are a step backwards because they allow you to ‘pollute’ your interfaces with code. But they provide the most elegant and practical way to allow backwards compatibility. It made it much easier for Oracle to update all the Collections classes and for you to retrofit your existing code for Lambda.
public class MyClass implements InterfaceA { * @param args the command line arguments public static void main(String[] args) { // TODO code application logic here @Override public void saySomething() { System.out.println("Hello World"); interface InterfaceA { public void saySomething(); default public void sayHi() { System.out.println("Hi"); }
Note that we have to provide implementation for all default methods. So default methods provides us the flexibility to allow methods to be implemented in interfaces. The implementation will be used as default if a concrete class does not provide implementation for that method.
Conflicts with Multiple Interface.Since classes in java can implement multiple interfaces, there could be a situation where 2 or more interfaces has a default method with the same signature hence causing conflicts as java will not know what methods to use at a time. This will then result in a compilation error with the message MyClass inherits unrelated defaults for sayHi() from types InterfaceA and InterfaceB.
Lets take a look at the example below.
public class MyClass implements InterfaceA, InterfaceB { * @param args the command line arguments public static void main(String[] args) { // TODO code application logic here @Override public void saySomething() { System.out.println("Hello World"); interface InterfaceA { public void saySomething(); default public void sayHi() { System.out.println("Hi from InterfaceA"); interface InterfaceB { default public void sayHi() { System.out.println("Hi from InterfaceB"); }
In order to work around situations like this,We will have to provide implementation for sayHi() method in the class MyClass therefore overriding both methods in InterfaceA and InterfaceB.
public class MyClass implements InterfaceA, InterfaceB { * @param args the command line arguments public static void main(String[] args) { // TODO code application logic here @Override public void saySomething() { System.out.println("Hello World"); @Override public void sayHi() { System.out.println("implemetation of sayHi() in MyClass"); interface InterfaceA { public void saySomething(); default public void sayHi() { System.out.println("Hi from InterfaceA"); interface InterfaceB { default public void sayHi() { System.out.println("Hi from InterfaceB"); }
If we want to specifically invoke one of the sayHi() methods in either InterfaceA or InterfaceB, we can also do as follows:
public class MyClass implements InterfaceA, InterfaceB { * @param args the command line arguments public static void main(String[] args) { // TODO code application logic here @Override public void saySomething() { System.out.println("Hello World"); @Override public void sayHi() { InterfaceA.super.sayHi(); interface InterfaceA { public void saySomething(); default public void sayHi() { System.out.println("Hi from InterfaceA"); interface InterfaceB { default public void sayHi() { System.out.println("Hi from InterfaceB"); }
Hopefully you have found this quick guide useful.
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process java.lang.RuntimeException: Unable to create application com.ads.app.MyApplication: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.ads.app. Make sure to call FirebaseApp.initializeApp(Context) first.
相关文章
- Jmeter里的全能java测试sampler - Java request
- Java实现 LeetCode 153 寻找旋转排序数组中的最小值
- Java实现第八届蓝桥杯方格分割
- java实现算年龄
- java实现手机尾号评分
- Java实现最大连续子数组和
- Java实现 蓝桥杯VIP 算法训练 Hanoi问题
- 目录 1.1. JVM内存模型总体架构图1 1.2. JAVA堆2 1.3. 方法区 元空间(Metaspace)2 1.4. 虚拟机栈3 1.5. 本地方法区4 2. 垃圾回收算法4 2
- 当年的Java考试:JAVA&移动应用&大数据-大三-社区疫苗接种管理系统(全部源码·保姆式呵护)
- Java Objects.isNull() 方法及代码示例
- Arrays.asList踩坑——引发的Exception in thread “main“ java.lang.UnsupportedOperationException
- java起源和基本数据类型
- Effective java读书札记第一条之 考虑用静态工厂方法取代构造器
- JAVA编程:java环境安装和helloworld
- Java通过PDF模板导出数据 adobe acrobat的PDF编辑器 itextpdf java导出文件输出流
- Java开发技术之成为高级java工程师必须学习的三个技术
- java 提取主域名
- 【java】Java并发编程--Java实现多线程的4种方式