Java Learning Notes Interface

Source: Internet
Author: User

First, the concept and definition of the interface

Consider the definition of a simple interface first:

 Public Interface output{    int max_line = +;     void out ();     void GetData (String msg);}
    • define interface using keyword interface
    • modifier public can be omitted, if omitted, the default access control is used, that is, only code in the same package structure can access this interface
    • interface can not have a constructor method (different from the constructor method in the Class)
    • All members of the interface, including constants, methods, and so on, are public access, so when defining a member of an interface, you can omit the access control modifier, not all at once, but only with the Span style= "color: #0000ff;" >public
    • can only be constant , and all have public, Static, final three properties, so inside the interface, the following two lines of statements are completely equivalent:
int Max_line = +;  Public Static Final Max_line = 40;

    • the method in the interface can only be an abstract method, that is, only declare the function type, name and parameter list, but can not provide a concrete implementation

Second, the inheritance of the interface

Consider the following code:

1 InterfaceA2 {3     intProp_a = 5;4     voidTestA ();5 }6 InterfaceB7 {8     intProp_b = 6;9     voidTestb ();Ten } One InterfaceCextendsA, B A { -     intProp_c = 7; -     voidTESTC (); the } -  -  Public classmyjavatest - { +      Public Static voidMain (string[] args) -     { + System.out.println (c.prop_a); A System.out.println (c.prop_b); at System.out.println (c.prop_c); -          -     } -}

Output:

5

6

7

    • The inheritance of the interface and the inheritance of the class are different, the interface supports multiple inheritance, multiple parent interfaces are queued after the extends keyword, and multiple parent interfaces are separated by commas, as shown in the 11 rows above, and Subinterface C inherits the parent interface A and B
    • After the child interfaces inherit the class parent interface, they can gain access to their member constants. such as 21-23 rows through Subinterface C can access the parent interface, a member of the constant prop_a and prop_b

Third, the implementation and definition of the interface

Consider the following procedure:

1 InterfaceOutput2 {3     intMax_line = 4;4     voidout ();5     voidGetData (String msg);6 }7 8 InterfaceProduct9 {Ten     intgetproducttime (); One } A  - //implement a simple print job -  Public classPrinterImplementsOutput, Product the { -     Privatestring[] Printdata =NewString[max_line]; -     Private intDatanum = 0;//record the number of jobs that need to be printed -      +      Public voidout () -     { +          while(Datanum > 0) A         { atSYSTEM.OUT.PRINTLN ("printer print" + printdata[0]); -              -             //move the job queue whole forward one bit and subtract 1 of the remaining jobs -System.arraycopy (Printdata, 1, printdata, 0,--datanum); -         } -          in     } -  to @Override +      Public intGetproducttime () { -         //TODO auto-generated Method Stub the         return45; *     } $ Panax Notoginseng @Override -      Public voidGetData (String msg) the     { +         if(Datanum >=max_line) A         { theSYSTEM.OUT.PRINTLN ("Output queue is full, add failed"); +         } -         Else $         { $             //Add print data to the queue with 1 job count -printdata[datanum++] =msg; -         }     the     } -      Public Static voidMain (string[] args)Wuyi     { the         //Create a Printer object that is used as output -Output O =NewPrinter (); WuO.getdata ("123"); -O.getdata ("456"); AboutO.getdata ("789"); $O.getdata ("ABC")); -O.getdata ("Def"); - o.out (); -         //Create a Printer object that is used as product AProduct P =NewPrinter (); + System.out.println (P.getproducttime ()); the     } -}

Output:

Output queue is full, add failed
Printer Printing 123
Printer Printing 456
Printer Printing 789
Printer Print ABC
45

  • An interface cannot be instantiated, but an interface can declare a variable of a reference type , such as 53 rows, the interface output declares a reference variable o is of type output,61, and interface product declares that the type of a reference variable p is product.
  • the implementation of the interface requires a class , usually in the following form:
    class extends Implements interface 1, interface 2: {    class body part  }

    Implements is a keyword that indicates that the class implements the interface, a class can implement multiple interfaces, and multiple interfaces are separated by commas, as shown in the above program 14 line

  • A class can inherit a parent class and implement multiple interfaces at the same time, where theimplements part must be placed after the extends part
  • After a class declaration implements one or more interfaces, the class must fully implement all of the abstract methods defined in those interfaces (the abstract method is those that are only declared and not implemented), otherwise the class retains the abstract method inherited from the parent interface. The class must therefore also be defined as an abstract class.

The above program implements the output interface and the product interface with class printer, so that the printer object can be assigned either to the output variable (53 rows) or to the product variable (61 rows), as if the printer class is both a subclass of the output class, is also a subclass of the output class, which is the legendary Java simulation multiple inheritance .

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.