Features of the interface in Java

Source: Internet
Author: User

The Java interface has undergone significant changes since 1.8. So the Java interface features can be divided into 1.8 versions before and after 1.8 versions.

Features prior to version 1.8:

    • An interface can have only static global constants and public-modified abstract methods.
    • For the sake of brevity, the methods in the interface can be modified without public and abstract, but the compiled method is still the public abstract type method. Global constants can also be decorated with public static fianl, which is still the public static final type after compilation.

The following code demonstrates a way to define an interface before the 1.8 version:

 Public Interface iflyable{    = "Static constant";     void

Although the above code does not use public static final to modify info, the compiled info is a static global constant, and the value cannot be modified externally. The same fly () is not decorated with public, but the compilation result is the public abstract type.

Here is the test code:

1  PackageOrg.lyk.vo;2 3 Importorg.lyk.intefaces.*;4 5  Public classBirdImplementsiflyable6 {7      Public voidFly ()8     {9System.out.println ("The Birds are flying");Ten     }   One}
1  PackageOrg.lyk.main;2 3 Importorg.lyk.intefaces.*;4 ImportOrg.lyk.vo.*;5 6  Public classHello7 {8      Public Static voidMain (string[] args)9     {TenIflyable flyable =NewBird (); One Flyable.fly ();  A System.out.println (iflyable.info); -     } -}

Features after the 1.8 release:

    • The limit of the global static constants and public abstract methods can only be found in the interface before 1.8 is broken. An entity method that allows an interface to be decorated with the default keyword (the method can be overwritten in subclasses)
    • An entity function that defines the public static type in the interface is also allowed, which is called by the interface.
1  Packageorg.lyk.intefaces;2 3  Public Interfaceiflyable4 {5      Public Static FinalString info = "Static constant";6      Public Abstract voidfly ();7     8      Public Static voidshowsomething ()9     {TenSystem.out.println ("This is a static function supported by the Java1.8, which can only be called by the interface"); One     } A      -     default voidprintsomething () -     { theSystem.out.println ("This is the default entity function that is supported by Java1.8.) This method can be overridden in subclasses. "); -     } -}

Test code:

1  PackageOrg.lyk.vo;2 3 Importorg.lyk.intefaces.*;4 5  Public classBirdImplementsiflyable6 {7      Public voidFly ()8     {9System.out.println ("The Birds are flying");Ten     }  One      A      Public  voidprintsomething () -     { -SYSTEM.OUT.PRINTLN ("Overwrite interface methods in subclasses"); the     } -}
1  PackageOrg.lyk.main;2 3 Importorg.lyk.intefaces.*;4 ImportOrg.lyk.vo.*;5 6  Public classHello7 {8      Public Static voidMain (string[] args)9     {TenIflyable flyable =NewBird (); One Flyable.fly ();  A System.out.println (iflyable.info); -          -          the flyable.printsomething (); - iflyable.showsomething (); -     } -}

PostScript: I think the reason why Java1.8 made such a big design change should be to learn from. NET, with the intention of supporting streams. Since Java was initially designed without taking into account the presence of stream, it is now necessary to break the existing limits by adding functionality to the interface.

Features of the interface in Java

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.