Java Vamei Quick Tutorial 05 Implementation Interface

Source: Internet
Author: User

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

In encapsulation and interfaces, the private keyword encapsulates an internal member of an object. Encapsulated, the product hides the internal details and is provided only to the user interface (interface).

Interfaces are very useful concepts that can assist in our abstract thinking. In real life, when we think of an appliance, we often think of the functional interface of the appliance. For example cups, we think of the possibility of adding water and water, higher than the thought of the glass material and price. That is to say, to a certain extent, the interface of the appliance is equivalent to the appliance itself. Internal details are discarded during the thought process.

A cup in mind

In the encapsulation mechanism of public and private, we actually define both classes and interfaces, and the classes and interfaces are mixed together. Java also provides the syntax for interface. This syntax strips the interface from the concrete definition of the class and forms an independent body.

Interface

In the case of cups, define the interface of a Cup:

Interface Cup {    void addwater (int w);    void Drinkwater (int w);}

Cup This interface defines a prototype of two methods (stereotype): Addwater () and Drinkwater (). The prototype of a method specifies the method name, the argument list, and the return type. Prototypes can tell the outside how to use these methods.

In the interface, we

    • You do not need to define the body of a method
    • No need to describe the visibility of the method

Note 2nd, the methods in interface default to public. As we mentioned in the encapsulation and interface, the public method of a class forms an interface. Therefore, all methods that appear in the interface default to public.

We can implement interfaces in the definition of a class, such as the following Musiccup (a cup that can play music):

Class Musiccup implements Cup {public    void Addwater (int w)     {        This.water = this.water + W;    }    public void Drinkwater (int w)    {        this.water = this.water-w;    }    private int water = 0;}

We use the Implements keyword to implement interface. Once a interface has been implemented in the class, all methods of Interface (Addwater () and Drinkwater ()) must be defined in the class. The methods in the class need to match the method prototypes in the interface. Otherwise, Java will error.

Other public methods that are not mentioned in interface can be defined in a class. In other words, interface specifies a minimum interface that must be implemented. For example, the following watercontent () method does not prescribe a prototype in the Cup interface:

Class Musiccup implements Cup {public    void Addwater (int w)     {        This.water = this.water + W;    }    public void Drinkwater (int w)    {        this.water = this.water-w;    }    public int watercontent ()    {        return this.water;    }    private int water = 0;}

The meaning of separating interfaces

We used the interface, but the interface didn't reduce the amount of work we had when we defined the class. We still have to write the class exactly as before. We should be even more careful not to violate the interface rules. So why should we use interface?

In fact, interface is like an industry standard. A factory (class) can adopt the industry standard (implement Interface), or it can not adopt the industry standard. However, a product that incorporates industry standards will have the following benefits:

    • Higher quality: Cups that do not have a water-adding function do not meet the standard.
    • Easier to promote: like a USB interface on a computer, downstream products can be more easily bridged.

If we already have a Java program that handles objects that match the CPU interface, such as picking a child to drink. So, as long as we are sure, we give the child's Cup (object) to implement a cup interface, you can ensure that children can perform water this action. As for how the Cup (object) defines the action of drinking water, we can leave it to the appropriate class to decide (for example, to drink water with a straw, or to open a small mouth to drink water).

In computer science, interfaces are very important concepts. For example, any operating system that provides UNIX interfaces can be called UNIX systems. Linux,mac Os,solaris are UNIX systems that provide similar interfaces. However, the specific implementation of each system (source code) is different. Linux is open source and you can see every line of code, but you still don't know how to write a Solaris system.

The same UNIX interface

Implementing multiple interfaces

A class can implement more than one interface. For example, we have the following interface:

Interface Musicplayer {    void play ();

Let's consider the Musiccup class again. Musiccup can be seen as a mixture of players and cups.

So Musiccup should have two sets of interfaces, i.e. implement both the Musicplayer interface and the Cup interface:

Class Musiccup implements Musicplayer, cup{public    void Addwater (int w)     {        This.water = this.water + W;    } Public    void Drinkwater (int w)    {        this.water = this.water-w;    }    public void Play ()    {        System.out.println ("la...la...la");    }
private int water = 0;}

Finally, you can try to put the interface and class definitions in this article in the same file, and write the test class to run.

Summarize

Interface, method stereotype, public

Implements interface

Implements Interface1, Interface2

Java Vamei Quick Tutorial 05 Implementation Interface

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.