Java Basic Tutorial Implementation interface _java

Source: Internet
Author: User

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

Interfaces are very useful concepts that can assist us in abstract thinking. In real life, when we think of an appliance, we often think of the functional interface of the appliance. such as cups, we think of the possibility of adding water and drink, 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 in the process of thinking.

A cup in mind

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

Interface

Take the cup as an example, define the interface of a Cup:

Copy Code code as follows:

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

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

In the interface, we

1. Do not need to define the body of the method
2. No need to explain the visibility of the method

Note that in the 2nd, the method in interface defaults to public. As we mentioned in the encapsulation and interface, the public method of a class forms the interface. Therefore, all methods appearing in 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):

Copy Code code as follows:

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 you have implemented a interface in a class, you must define all the methods of Interface (Addwater () and Drinkwater ()) in the class. The methods in the class need to match the method prototypes in the interface. Otherwise, Java will complain.

In a class, you can define other public methods that interface does not mention. In other words, interface a minimum interface that must be implemented. For example, the following watercontent () method does not specify a prototype in the Cup interface:

Copy Code code as follows:

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 the separation interface

We used interface, but this interface doesn't reduce the amount of work we do when we define classes. We still have to write the class exactly as before. We should even be more careful not to violate the rules of interface. That being the case, why do we use interface?

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

1. Higher quality: No water function cup does not conform to the standard.
2. Easier to promote: Just like the USB interface on the computer, downstream products can be more easily connected.

If we already have a Java program that handles objects that match the Cup interface, such as getting the kids to drink water. So, as long as we are sure that we have a cup interface for the children's cups, we can ensure that the child can perform the action of drinking water. As for how the Cup (object) specifically 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 a very important concept. For example, any operating system that provides a UNIX interface can be called a UNIX system. 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, you can view 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 one of the following interface:

Copy Code code as follows:

Interface Musicplayer {
void Play ();
}

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

So Musiccup should have two sets of interfaces, that is, the simultaneous implementation of the Musicplayer interface and the Cup interface:

Copy Code code as follows:

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, write the test class, and run it.

Summarize

Interface, method stereotype, public

Implements interface

Implements Interface1, Interface2

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.