Java basic 05 implementation Interface

Source: Internet
Author: User

We use the public and private keywords in encapsulation and interfaces to encapsulate object members. After encapsulation, the product hides internal details and only provides them to user interfaces ).

An interface is a very useful concept that assists us in abstract thinking. In real life, when we think of an appliance, we often think of its functional interface. For example, the possibility of adding water and drinking water to a cup is higher than the material and price of the cup. That is to say, we equate the interface of the appliance with the concept of the appliance to a certain extent, while the internal details are abandoned in the process of thinking.

 

A cup in mind

 

In the encapsulation mechanism of public and private, we define both classes and interfaces. Java also provides the interface syntax. This syntax separates the interface from the specific definition of the class to form an independent entity.

 

Interface
Take a cup as an example to define a cup interface:

Interface Cup {
Void addWater (int w );
Void drinkWater (int w );
} The Cup interface defines two prototype methods (stereotype): addWater () and drinkWater (). The prototype of a method specifies the method name, parameter list, and return type. A prototype can tell external users how to use these methods.

In the interface, we

• The subject of the method does not need to be defined
• Method visibility is not required
Note that the method in interface is public by default. As we mentioned in encapsulation and interface, the public method of a class constitutes an interface. Therefore, all methods that appear in the interface are public by default.

 

We can implement interfaces in the definition of a class, such as the following MusicCup (which 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 the interface. Once an interface is implemented in the class, all methods (addWater () and drinkWater () of the interface must be defined in the class ()). The methods in the class must be consistent with the method prototype in the interface. Otherwise, Java reports an error.

 

In the class, you can define other public methods not mentioned by the interface. That is, the interface specifies the minimum interface that must be implemented. For example, the following waterContent () method does not specify the 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;
}

Significance of separating Interfaces
We use the interface, but this interface does not reduce the workload when we define the class. We still need to write classes as before. We should be more careful not to violate the interface rules. In this case, why should we use interfaces?

In fact, interfaces are like industry standards. A factory (class) can adopt industrial standards (implement interface) or not. However, a product that adopts industry standards will have the following benefits:

• Higher Quality: cups without the water adding function do not comply with standards.
• Easier promotion: downstream products can be more easily connected, just like USB interfaces on computers.
If we already have a Java program that is used to process objects that comply with the Cup interface, such as drinking water for children. Then, as long as we confirm, we have implemented a Cup interface for the child's Cup (object) to ensure that the child can perform the drinking water action. As for how the cup (object) defines the action of drinking water, we can leave it to the appropriate class (for example, using a straw to drink water or opening a mouth to drink water ).

Interfaces are an important concept in computer science. For example, any operating system that provides UNIX interfaces can be called a UNIX system. Linux, Mac OS, and Solaris are all UNIX systems that provide similar interfaces. However, the implementation (Source Code) of each system 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.

 

Same UNIX Interface

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

Interface MusicPlayer {
Void play ();
}

Let's look at the MusicCup class. MusicCup can be seen as a mixture of players and cups.

 

Therefore, MusicCup should have two sets of interfaces: 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 ");
}
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.

 

Summary
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.