Java Interface Example

Source: Internet
Author: User
Tags abstract final inheritance

The "Interface" (interface) keyword makes the concept of abstraction deeper into one layer. We can think of it as a "pure" abstract class. It allows the creator to specify the basic form of a class: The method name, the argument list, and the return type, but not the method body. Interfaces also contain data members of the base data 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 like this: "For all the classes that implement me, it should look like I am." Therefore, all code that takes a particular interface knows what methods might be invoked for that interface. This is the full meaning of the interface. So we often use interfaces to create a "protocol" between classes. Some object-oriented programming languages use a keyword called "Protocol" (protocol), which does the same thing as the interface.
To create an interface, use the interface keyword instead of class. Like a class, we can add a public keyword before the interface keyword (but only the interface is defined in a file with the same name), or omit it to create a "friendly" state.
To generate classes that match a particular interface (or a set of interfaces), use the implements (Implementation) keyword. What we're going to say is, "the interface looks just like that, and here's the specifics of its work." In addition to these, our other work is very similar to the inheritance. Here is a schematic of the instrument example:

After the implementation of an interface, a common class is obtained, which can be extended in a 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 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 limit our access to a method during inheritance--java compiler does not allow us to do that.
This can be clearly seen in the modified version of the instrument example. Note that each method in the interface is strictly a declaration, which is the only allowed by the compiler. In addition, none of the INSTRUMENT5 methods are declared public, but they automatically get the public attribute. As shown below:

: Music5.java//interfaces import java.util.*; Interface Instrument5 {//compile-time constant:int i = 5;//static & final//cannot have method definitions : void Play ();
  Automatically public String what ();
void adjust ();
  Class Wind5 implements INSTRUMENT5 {public void play () {System.out.println ("Wind5.play ()");
  Public String What () {return "Wind5";} public void adjust () {}} class Percussion5 implements INSTRUMENT5 {public void play () {System.out.println ("Percu
  Ssion5.play () ");
  Public String What () {return "Percussion5";} public void adjust () {}} class Stringed5 implements INSTRUMENT5 {public void play () {System.out.println ("Stringe
  D5.play () ");
  Public String What () {return "Stringed5";}
  public void adjust () {}} class Brass5 extends Wind5 {public void play () {System.out.println ("Brass5.play ()");
  public void adjust () {System.out.println ("brass5.adjust ()"); } class Woodwind5 EXtends Wind5 {public void play () {System.out.println ("Woodwind5.play ()");

Public String What () {return ' Woodwind5 ';}} public class Music5 {//doesn ' t care about type, so new types//Added to the system still work right:static void
  Tune (INSTRUMENT5 i) {//... i.play ();
  } static void Tuneall (instrument5[] e) {for (int i = 0; i < e.length; i++) tune (e[i));
    public static void Main (string[] args) {instrument5[] Orchestra = new INSTRUMENT5[5];
    int i = 0;
    upcasting during addition to the array:orchestra[i++] = new Wind5 ();
    orchestra[i++] = new Percussion5 ();
    orchestra[i++] = new Stringed5 ();
    orchestra[i++] = new BRASS5 ();
    orchestra[i++] = new Woodwind5 ();
  Tuneall (orchestra); }
} ///:~

The rest of the code works the same way. We can freely decide to trace 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, "abstract" class, or an "interface". This is done intentionally: Each method enables the programmer to have different control over the creation and use of the object.

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.