Java 8 new Features-Interface definition enhancements

Source: Internet
Author: User

In order to solve the existing interface in the method is insufficient, add a new method to the original interface , n multi-implementation class under the interface also need to rewrite the method of the problem! JAVA8 introduces an interface definition enhancement concept!

Java8 broke the method definition of the original interface: Public, abstract

  • Default Method
    • Java 8 also allows us to add a non-abstract method implementation to the interface, just using the default keyword , which is also called the extension method.
    • When the interface is implemented, the default extension method can be used directly on subclasses, which are used in a manner similar to non-abstract member methods in abstract classes.
    • However, the extension method is not able to overload the method in Object. For example: toString, equals, hashcode cannot be overloaded in the interface.
    • For example:
    • Interfaceimassage{ Public Abstract voidPrint ();//This is the original definition of the interface.     Public default voidFun () {//define a common method in the interfaceSystem.out.println ("Use the default keyword to define the common method in the interface!" "); }}classMessageimplImplementsimassage{@Override Public voidprint () {System.out.println ("The implementation class of the interface! "); }}   
      • Test:
         public  class   Newinterface { public  Span style= "color: #0000ff;" >static  void   main (string[] args) {Imassa        GE massage  = new   Messageimpl ();  //  Normal method implementations call   Massage.print ();  //  Call the normal method in the interface   Massage.fun (); }}
      • Results:
    • Static methods
      • The other is to define a static method in the interface, using the static keyword
      • For example:
        Interface   imassage{    publicstaticvoid  funstatic () {        SYSTEM.OUT.PRINTLN ("Use static keyword to define static method in Interface");}    }

      • Called in the same way as a normal static method call
      •    // call the static method   defined in the interface Imassage.funstatic ();

Java 8 new Features-Interface definition enhancements

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.