Java Default usage

Source: Internet
Author: User

We all know that you can only define method names in the interface of the Java language, not the specific implementation code of the method. The methods defined in the interface must be implemented in the non-abstract subclasses of the interface. Here is an example of an interface:

Public interface Simpleinterface {public  void Dosomework ();} Class Simpleinterfaceimpl implements simpleinterface{  @Override public  void Dosomework () {    System.out.println ("Do Some work implementation in the class");  }  public static void Main (string[] args) {    Simpleinterfaceimpl simpobj = new Simpleinterfaceimpl ();    Simpobj.dosomework ();  }}

So what happens if we add a new method to the Simpleinterface?

Public interface Simpleinterface {public  void dosomework ();  public void Dosomeotherwork ();}

If we try to compile the code above, we will get the following result:

$javac. \simpleinterface.java.\simpleinterface.java:18:error:simpleinterfaceimpl isn't abstract and does not override Abstract method Dosomeotherwork () in Simpleinterfaceclass Simpleinterfaceimpl implements SIMPLEINTERFACE{^1 error

Because the interface has this syntax constraint, it is very difficult to directly change/extend the methods within the interface. When we tried to harden the Java 8 collections API to support lambda expressions, we faced the challenge. To overcome this difficulty, Java 8 introduces a new concept called the default method, which can also be referred to as the Defender method, or virtual extension method (Vsan extension methods).

The default method refers to the inclusion in the interface of some of the defaults of the implementation of the method (that is, the interface can contain the method body, which breaks the Java version of the interface syntax limitations), so that the interface in the extension of the time, will not break the interface-related implementation class code. Next, let's look at an example:

Public interface Simpleinterface {public  void dosomework ();  A default method in the interface created using "default" keyword  //using the DEFAULT keyword created in interface to create a default approach directly, The method contains the specific implementation code of  default public void Dosomeotherwork () {    System.out.println ("Dosomeotherwork implementation in The interface ");}  } Class Simpleinterfaceimpl implements simpleinterface{  @Override public  void Dosomework () {    System.out.println ("Do Some work implementation in the class");  }  /*   * required to override to provide a implementation    * for dosomeotherwork.   * In Simpleinterfaceimpl, there is no need to implement the Dosomeotherwork method defined in the interface *  /public static void main (string[] args) {    Simpleinterfaceimpl simpobj = new Simpleinterfaceimpl ();    Simpobj.dosomework ();    Simpobj.dosomeotherwork ();  }}

The output of this program is:

Do Some work implementation in the Classdosomeotherwork implementation in the interface

The above is a very brief introduction to the default method. If you are interested, you can also read this document to get a deeper understanding.

Update:

One of the most common questions to ask is: If a class implements two interfaces (which can be seen as "multiple inheritance"), and both interfaces contain a default method with the same name, what happens? In this case, the compiler will make an error. Let me use an example to explain:

Public interface Interfacewithdefaultmethod {public  void SomeMethod ();  Default public void Someothermethod () {    System.out.println ("default method implementation in the interface");}  } Public interface Interfacewithanotherdefmethod {  default public void Someothermethod () {    System.out.println (" Default method implementation in the interface ");}  }

Then we define a class that implements the above two interfaces:

public class Defaultmethodsample implements   Interfacewithdefaultmethod, interfacewithanotherdefmethod{  @ Override public  void SomeMethod () {    System.out.println ("Some method implementation in the class");  public static void Main (string[] args) {    defaultmethodsample def1 = new Defaultmethodsample ();    Def1.somemethod ();    Def1.someothermethod ();  }  }

If you compile the above code, you will get a compiler error, as shown below. Because the compiler does not know which one to choose in the two default methods with the same name, it generates two semantics.

Java Default usage

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.