A detailed description of the default method for Java 8

Source: Internet
Author: User

A detailed description of the default method for Java 8

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

Java 8 Adds the default method, which can add new feature features to an interface without affecting the implementation class of the interface. Let's illustrate this by 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 ();}
The above code shows that the MyClass class implements the SaySomething () method of the Interfacesa interface. Now we have a new Sayhi () method for the Interfacesa interface. In doing so, the MyClass class is not compiled unless we provide the implementation of the Sayhi ().

The default method is very useful, and the implementation class does not need to provide the implementation of the method by adding the keyword default before the access modifier of the method defined by the interface. Like what:

public class MyClass implements Interfacea {public static void main (string[] args) {} @Overridepublic void saysomething () {/ /TODO auto-generated method stub}}interface interfacea{public void saysomething ();d efault 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 written in the interface. The implemented method is implemented as the default method.

So what if there is a conflict between the multiple interfaces?

Because Java classes can implement multiple interfaces, there may be situations where two or more interfaces have a default interface method with the same name, which creates a conflict. Because a Java virtual machine is not clear as to which default method you want to use while the program is running. This can cause compilation errors.

Let's take a look at the following example.

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 () ;d efault 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 will report the following error:

"Duplicate default methods named Sayhi with the parameters () and () is inherited from the types Interfaceb and Interface A. "

Unless the Sayhi () method is overridden 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 ();d efault 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 which interface's Sayhi () method 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 ();d efault public void Sayhi () {System.out.println (' Hi from Interfacea ');}} Interface Interfaceb{default public void Sayhi () {System.out.println (' Hi from Interfaceb ');}}
Is the answer not very simple?


A detailed description of the default method for Java 8

Related Article

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.