Java 8 default method details

Source: Internet
Author: User

Java 8 default method details
Java 8 default method details

Author: chszs, reprinted with note. Blog homepage: http://blog.csdn.net/chszs

Java 8 adds the default method, which can add new features to the interface without affecting the interface implementation class. The following is an example to illustrate this point.

public class MyClass implements InterfaceA {public static void main(String[] args){}@Overridepublic void saySomething() {// TODO Auto-generated method stub}}interface InterfaceA{public void saySomething();}
The code above shows that the MyClass class implements the saySomething () method of the InterfacesA interface. Now we add a sayHi () method for the InterfacesA interface. In this case, the MyClass class cannot be compiled unless we provide the sayHi () implementation method.

The Default method is very useful. By adding the keyword default before the access modifier of the method defined by the interface, the implementation class does not need to provide the implementation of this method. For example:

public class MyClass implements InterfaceA {public static void main(String[] args){}@Overridepublic void saySomething() {// TODO Auto-generated method stub}}interface InterfaceA{public void saySomething();default public void sayHi(){System.out.println("Hi");}}
Note that we must provide the implementation of all default methods. Therefore, the default method makes our code more flexible and can be implemented by writing methods in the interface. The implementation method is implemented as the default method.

So what should I do if there are conflicts between multiple interfaces?

Because 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, resulting in a conflict. Because when the Java virtual machine is running, it is not clear which default method you want to use. This will cause compilation errors.

Let's take a look at the example below.

public class MyClass implements InterfaceA, InterfaceB {public static void main(String[] args){MyClass mc = new MyClass();mc.sayHi();}@Overridepublic void saySomething() {// TODO Auto-generated method stub}}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");}}
It is not compiled, and the following error is reported:

"Duplicate default methods named sayHi with the parameters () and () are inherited from the types InterfaceB and InterfaceA ."

Unless the sayHi () method is rewritten in the MyClass class:

public class MyClass implements InterfaceA, InterfaceB {public static void main(String[] args){MyClass mc = new MyClass();mc.sayHi();}@Overridepublic void saySomething() {// TODO Auto-generated method stub}@Overridepublic 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 you want to specify the sayHi () method of which interface to call, we can do this:

public class MyClass implements InterfaceA, InterfaceB {public static void main(String[] args){MyClass mc = new MyClass();mc.sayHi();}@Overridepublic void saySomething() {// TODO Auto-generated method stub}@Overridepublic 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");}}
Is the answer simple?


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.