Java Foundation 05 Implementing Interfaces

Click on " Java Head " above and select "Public Account"

Dry goods articles are delivered as soon as possible!


In Java Foundation 04 Encapsulation and Interfaces , the private keyword encapsulates the internal members of an object. After encapsulation, the product hides the internal details and only provides it to the user interface .


Interfaces are very useful concepts to aid our abstract thinking. In real life, when we think of an appliance, we often think of the functional interface of the appliance. For example, in cups, we think about the possibility of adding water and drinking water, rather than thinking about the material and price of the cup. That is, to a certain extent, the interface of the appliance is equivalent to the appliance itself. Internal details are discarded in the thinking process.


640?wxfrom=5&wx_lazy=1

a cup in mind

 

In the encapsulation mechanism of public and private, 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 concrete definition of the class and forms a separate body.

 

interface



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


interface Cup {
   void addWater(int w);
   void drinkWater(int w);
}


The Cup interface defines the prototype of two methods (stereotype): addWater() and drinkWater(). A method's prototype specifies the method name, parameter list, and return type. The prototype can tell the outside how to use these methods.


In the interface, we


  • There is no need to define the body of the method

  • No need to state the visibility of the method


Note the second point, the methods in the interface are public by default. As we covered in Java Foundation 04 Encapsulation and Interfaces , the public methods of a class constitute an interface. Therefore, all methods appearing in the interface are public by default .

 

We can implement the interface in a class definition, such as the following MusicCup (a mug 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 the interface. Once an interface is implemented in a class, all methods of the interface (addWater() and drinkWater()) must be defined in that 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. That is, interface specifies a minimum interface that must be implemented. For example, the following waterContent() method does not specify 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 separate interfaces



We use the interface, but this interface does not reduce the workload when we define the class. We still have to write specific classes as before. We even have to be more careful not to violate the rules of the interface. That being the case, why do we use interface?


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


  • Higher quality: cups without water filling function are not up to standard.

  • Easier to promote: Just like the USB port on a computer, downstream products can be connected more easily.


If we already have a Java program that handles objects that conform to the Cup interface, such as leading 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 the cup (object) specifically defines the action of drinking water, we can leave it to the corresponding class to decide (such as drinking water with a straw, or taking a sip to drink water).


Interfaces are an important concept in computer science. For example, any operating system that provides a UNIX interface can be called a UNIX system. Linux, Mac OS, Solaris are all UNIX systems that provide similar interfaces. However, the specific implementation (source code) of each system differs from one another. Linux is open source, and you can look at every line of code, but you still don't know how to write a Solaris system.


640

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 consider the MusicCup class again. MusicCup can be seen as a hybrid of a player and a cup.

640

So MusicCup should have two sets of interfaces, that is to implement MusicPlayer interface and Cup interface at the same time:


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


  • Original text: cnblogs.com/vamei/archive/2013/03/27/2982230.html

640?

Head of Java

Daily sharing of Java dry goods

WeChat ID: javatuanzhang

640?

Long press to identify the QR code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325692187&siteId=291194637