In encapsulation and interfaces, the private keyword encapsulates the internal members of the object. After encapsulation, the product hides the internal details and only provides the user interface.
Interface is a very useful concept that can assist our abstract thinking. In real life, when we think of a certain appliance, we often think of the functional interface of the appliance. For example, with a cup, we think about the possibility of adding water and drinking water more than we think about the material and price of the cup. In other words, to a certain extent, the interface of the appliance is equivalent to the appliance itself. Internal details were discarded from the thought process.
a cup in mind
In the public and private encapsulation mechanisms, we actually define classes and interfaces at the same time, and classes and interfaces are mixed together. Java also provides the interface syntax. This syntax separates the interface from the specific definition of the class and forms an independent body.
interface
Taking a cup as an example, define a cup interface:
Copy the code code as follows:
interface Cup {
void addWater(int w);
void drinkWater(int w);
}
The Cup interface defines the prototypes of two methods: addWater() and drinkWater(). A method's prototype specifies the method name, parameter list, and return type. Prototypes can tell the outside world how to use these methods.
In the interface, we
1. No need to define the body of the method
2. There is no need to specify the visibility of the method
Note the second point, the methods in the interface are public by default. As we mentioned in Encapsulation and Interfaces, the public methods of a class constitute an interface. Therefore, all methods that appear in the interface are public by default.
We can implement the interface in the definition of a class, such as the MusicCup below (a cup that can play music):
Copy the 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 the interface. Once an interface is implemented in a class, all methods of the 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 report an error.
Other public methods not mentioned by the interface can be defined in the class. In other words, interface specifies a minimum interface that must be implemented. For example, the following waterContent() method does not specify a prototype in the Cup interface:
Copy the 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 separate interfaces
We used an interface, but this interface did not reduce our workload when defining classes. We still have to write classes specifically as before. We even have to be more careful not to violate the rules of the interface. In this case, why do we use interface?
In fact, the interface is like an industry standard. A factory (class) can adopt industry standards (implement interface) or not. However, a product that adopts industry standards will have the following benefits:
1. Higher quality: Cups without water-adding function do not meet the standards.
2. Easier promotion: Just like the USB interface on a computer, downstream products can be more easily connected.
If we already have a Java program, it is used to process objects that conform to the Cup interface, such as getting children to drink water. Then, as long as we are sure that we implement the Cup interface for the child's cup (object), we can ensure that the child can perform the action of drinking water. As for how this cup (object) specifically defines the action of drinking water, we can leave it to the corresponding class to decide (such as drinking through a straw, or opening a small mouth to drink).
In computer science, interface is a very important concept. For example, any operating system that provides a UNIX interface can be called a UNIX system. Linux, Mac OS, and Solaris are all UNIX systems and they provide similar interfaces. However, the specific implementation (source code) of each system is different from each other. Linux is open source, and you can view every line of its code, but you still don't know how to program a Solaris system.
Same UNIX interface
Implement multiple interfaces
A class can implement more than one interface. For example, we have the following interface:
Copy the code code as follows:
interface MusicPlayer {
void play();
}
Let's consider the MusicCup class again. MusicCup can be seen as a hybrid of a player and a cup.
Therefore, MusicCup should have two sets of interfaces, that is, implement the MusicPlayer interface and the Cup interface at the same time:
Copy the 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