Java 8 has added a default method, which can add new functional features to the interface without affecting the interface's implementation class. Let's illustrate this with examples below.
The code copy is as follows:
public class MyClass implements InterfaceA {
public static void main(String[] args){
}
@Override
public void saysSomething() {
// TODO Auto-generated method stub
}
}
interface Interface InterfaceA{
public void saysSomething();
}
The above code shows that the MyClass class implements the saySomething() method of the InterfacesA interface. Now we add a new sayHi() method to the InterfacesA interface. If this is done, the MyClass class cannot be compiled unless we provide the implementation method of sayHi().
The Default method is very useful. By adding the keyword default to the access modifier of the method defined by the interface, the implementation class does not need to provide an implementation of the method. for example:
The code copy is as follows:
public class MyClass implements InterfaceA {
public static void main(String[] args){
}
@Override
public void saysSomething() {
// TODO Auto-generated method stub
}
}
interface Interface InterfaceA{
public void saysSomething();
default public void saysHi(){
System.out.println("Hi");
}
}
Note that we must provide implementations of all default methods. Therefore, the default method makes our code more flexible and can also write methods to implement them in the interface. The implemented method will be implemented as the default method.
So, what should I do if there is a conflict in multiple interfaces?
Since Java classes can implement multiple interfaces, there may be a situation where two or more interfaces have a default interface method with the same name, causing conflicts. Because the Java virtual machine is running when the program is running, it is not clear which default method you want to use. This will cause a compilation error.
Let's take a look at the following example.
The code copy is as follows:
public class MyClass implements InterfaceA, InterfaceB {
public static void main(String[] args){
MyClass mc = new MyClass();
mc.sayHi();
}
@Override
public void saysSomething() {
// TODO Auto-generated method stub
}
}
interface Interface InterfaceA{
public void saysSomething();
default public void saysHi(){
System.out.println("Hi from InterfaceA");
}
}
interface Interface InterfaceB{
default public void saysHi(){
System.out.println("Hi from InterfaceB");
}
}
It cannot pass the compilation and will report the following error:
“Duplicate default methods named sayHi with the parameters () and () are inherited from the types InterfaceB and InterfaceA.”
Unless the sayHi() method is overridden in the MyClass class:
The code copy is as follows:
public class MyClass implements InterfaceA, InterfaceB {
public static void main(String[] args){
MyClass mc = new MyClass();
mc.sayHi();
}
@Override
public void saysSomething() {
// TODO Auto-generated method stub
}
@Override
public void saysHi(){
System.out.println("implementation of sayHi() in MyClass");
}
}
interface Interface InterfaceA{
public void saysSomething();
default public void saysHi(){
System.out.println("Hi from InterfaceA");
}
}
interface Interface InterfaceB{
default public void saysHi(){
System.out.println("Hi from InterfaceB");
}
}
If you want to specify which interface's sayHi() method to call, we can do this:
The code copy is as follows:
public class MyClass implements InterfaceA, InterfaceB {
public static void main(String[] args){
MyClass mc = new MyClass();
mc.sayHi();
}
@Override
public void saysSomething() {
// TODO Auto-generated method stub
}
@Override
public void saysHi(){
InterfaceA.super.sayHi();
}
}
interface Interface InterfaceA{
public void saysSomething();
default public void saysHi(){
System.out.println("Hi from InterfaceA");
}
}
interface Interface InterfaceB{
default public void saysHi(){
System.out.println("Hi from InterfaceB");
}
}
Is the answer very simple?