Definition of members in interfaces in Java

Source: Internet
Author: User

The purpose of the interface in Java is to provide a programming framework that extends other classes as a uniform specification and is a very good design in Java.

The boy uses the following code to summarize the members that interfaces in Java can define and the keywords they are decorated by default:

//the access modifier for the external interface can only be public or the default modifier, and its members can only be decorated with the public access modifier, and the interface cannot be final decorated Public InterfaceA {//member variables, default with public static final decorationString name= "Ahei"; //member methods, default with public abstract decoration    voidAbstractmethod (); //internal interface, default with public static decoration    Interfaceinterf{}//inner class, default with public static decoration    classinnerclass{}//static methods, which are modified by default with public, and static methods in the interface cannot be final decorated    Static voidStaticmethod () {}//The default method, which provides a method implementation, as a means of library, frame forward compatibility    default voidDefaultmethod () {System.out.println ("Haha"); }         Public Static voidMain (string[] args) {//The following description of the class members and interface members in the interface are the default static decorated        NewA.interf () {}; NewA.innerclass (); //The invocation of a static method is determined at compile time and is determined by the type of the declarationC c=NewD ();   C.set (); //Output C//The default method is called through the object        NewF (). Defaultmethod (); }}//static methods can also be overridden, but they belong to their own classes and do not have run-time polymorphismclassC {Static voidset () {System.out.println (C); }}classDextendsC {Static voidset () {System.out.println (D); }}classFImplementsa{@Override Public voidAbstractmethod () {}}

An enumeration class can also be defined in the Java interface, and the static and default methods in the interface are JAVA8 new, and the default method provides new functionality for the interface and does not affect the implementation of older versions of code, guaranteeing forward compatibility.

Definition of members in interfaces in Java

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.