Before Java 8, only abstract methods can be written in interfaces, but implementation methods cannot be written.
Java 8 can have methods to implement, you can add default methods and static methods to interfaces
The default method is default
decorated, which can only be used in the interface, the static method is static
decorated, and there can be multiple default methods and static methods in the interface at the same time.
Why use interface default methods
Take a very realistic example:
Our interface has been written a long time ago, and later due to various business problems, it is inevitable to modify the interface.
Before Java 8, for example, if you want to add an abstract method to an interface, then all interface implementation classes must implement this method, otherwise there will be a compilation error.
And some implementation classes do not need to implement this method at all and are forced to write an empty implementation, and the changes will be very large.
Therefore, the default method of the interface is to solve this problem. As long as a default method is added to an interface, all the implementation classes will be automatically inherited, and there is no need to change any implementation classes, and it will not affect the business;
In addition, interface default methods can be overridden by interface implementing classes.
Why have interface static methods?
Interface static methods are similar to default methods, except that interface static methods cannot be overridden by interface implementation classes.
Interface static methods can only be called directly through the 接口名
. where the static method is located.静态方法名
Interface default method multiple inheritance conflict problem
Because interface default methods can be inherited and overridden, if multiple inherited interfaces have the same default method, there is a conflict problem.
conflict one
interface People { default void eat() { System.out.println("People eat."); } } interface Man { default void eat() { System.out.println("Man eats."); } } //Boy inherits both People and Man, and an error will be reported in the IDEA editor at this time interface Boy extends People, Man { } //This is the conflict problem caused by multiple inheritance of interfaces. Boy doesn't know who to inherit. //This is obviously also a problem, and IDEA will also prompt that this method needs to be rewritten to solve the problem: interface Boy extends People, Man { @Override default void eat() { People.super.eat(); //The default method of the specified parent interface can also be called directly in the method Man.super.eat(); //The default method of the specified parent interface can also be called directly in the method System.out.println("The boy eats."); } }
Test it with the implementation class
class Student implements Boy { public static void main(String[] args) { Student student = new Student(); student.eat(); } } //result //People eat. // Men eat. //The boy eats.
conflict two
Let's change the way of writing, Man
inherit People
, and then Man
override People
the default method in
At this point, the editor does not report an error, and People
the default method is grayed out, indicating that it is not used.
Run the above example again and output:
interface People { default void eat() { System.out.println("People eat."); } } interface Man extends People{ default void eat() { System.out.println("Man eats."); } } interface Boy extends People, Man { } //result // man eats
Because Man
of inheritance People
, Man
the default method is reset. Obviously, at this time, Boy
we know whose default method to inherit.
Note that at this time, if you use People
the method super
of calling the parent class, an eat
error will be reported, because it cannot be called.
interface Boy extends People, Man { @Override default void eat() { People.super.eat(); //People report error System.out.println("The boy eats"); } }
conflict three
Add a new method to the interface Man
: say
, and then Boy
add a default method to the interface: say
.
interface Man extends People{ default void eat() { System.out.println("Man eats."); } void say(); //IDEA prompts that the say method is grayed out and not used }
At this time, Man
the abstract method in is actually ignored, and IDEA
it is prompted that it is not used. This is obviously the default method takes precedence over the abstract method.
Summarize
Introduces default methods and static methods in Java 8 , as well as solutions to conflicting default methods.
If it is said in the future that the interface cannot write the implementation method, it will be too much OUT
.