Interface--interface

Source: Internet
Author: User

The "Interface" (interface) keyword gives the abstraction a deeper layer of concept. We can think of it as a "pure" abstract class. It allows the creation
The creator prescribes the basic form of a class: The method name, the argument list, and the return type, but does not specify the method body. The interface also contains the base number
Data members of the type, but they are both static and final by default. Interfaces provide only one form and do not provide details of implementation.
The interface describes itself this way: "For all my classes to be implemented, it should look like I am now." As a result, a specific connection is used
All the code in the port knows what method might be called for that interface. This is the full meaning of the interface. So we often use the interface for building
A "protocol" between a class and a class. Some object-oriented programming languages use a key called "Protocol" (protocol)
Word, it does the same thing as the interface.
To create an interface, use the interface keyword instead of class. Similar to the class, we can do this before the interface keyword
Add a public keyword (but only the interface is defined in a file of the same name), or omit it to create a "friendly"
State.
To generate a class that conforms to a particular interface (or set of interfaces), use the implements (Implementation) keyword. The way we want to express
Think is "The interface looks like that, here is its specific work details." In addition to these, the rest of our work is highly
Similar. Here is an example of a musical instrument:


When an interface is implemented, an ordinary class is obtained, which can be extended in standard way.
You can decide to explicitly define a method declaration in an interface as "public". But even if they are not explicitly defined, they will default to public. So
When implementing an interface, the method from the interface must be defined as public. Otherwise, they will default to "friendly" and will
Restrict access to a method in the inheritance process the--java compiler does not allow us to do that.
In the revised version of the instrument example, you can clearly see this. Note that each method in the interface is strictly a declaration,
It is only allowed by the compiler. In addition, none of the methods in INSTRUMENT5 are declared public, but they are automatically obtained
Public property. As shown below:

//: Music5.java//Interfaces173ImportJava.util.*;InterfaceINSTRUMENT5 {//Compile-time constant:inti = 5;//Static & Final//cannot has the method definitions:voidPlay ();//automatically publicString What ();voidadjust ();}classWind5ImplementsINSTRUMENT5 { Public voidPlay () {System.out.println ("Wind5.play ()");} PublicString what () {return"Wind5"; } Public voidadjust () {}}classPercussion5ImplementsINSTRUMENT5 { Public voidPlay () {System.out.println ("Percussion5.play ()");} PublicString what () {return"Percussion5"; } Public voidadjust () {}}classStringed5ImplementsINSTRUMENT5 { Public voidPlay () {System.out.println ("Stringed5.play ()");} PublicString what () {return"Stringed5"; } Public voidadjust () {}}classBrass5extendsWind5 { Public voidPlay () {System.out.println ("Brass5.play ()");} Public voidAdjust () {System.out.println ("Brass5.adjust ()");}}classWoodwind5extendsWind5 { Public voidPlay () {System.out.println ("Woodwind5.play ()");} PublicString what () {return"Woodwind5"; }} Public classMUSIC5 {174//doesn ' t care on type, so new types//added to the system still:Static voidtune (Instrument5 i) {// ...i.play ();}Static voidTuneall (instrument5[] e) { for(inti = 0; i < e.length; i++) Tune (E[i]);} Public Static voidMain (string[] args) {instrument5[] Orchestra=NewInstrument5[5];inti = 0;//upcasting During addition to the array:orchestra[i++] =NewWind5 (); Orchestra[i++] =NewPercussion5 (); Orchestra[i++] =NewStringed5 (); Orchestra[i++] =NewBrass5 (); Orchestra[i++] =NewWoodwind5 (); Tuneall (orchestra);}} ///:~the rest of the code works the same way. We can freely decide to go back to a "normal" class called INSTRUMENT5, an "abstract" class called INSTRUMENT5, or an "interface" called INSTRUMENT5. All behaviors are the same. In fact, we can see in the tune () method that there is no evidence that Instrument5 is a "normal" class, an "abstract" class or an "interface." This is done intentionally: Each method enables programmers to control the creation and use of objects differently. 

Interface--interface

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.