Java Basics-Interface

Source: Internet
Author: User

One:Interface, English is called interface, in software engineering, the interface refers to the method or function for others to call.

in encapsulation and interfaces, theprivate 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.

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.

Two:Interface  Syntax:

    1. Interface interface Name {}

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

Interface Cup {    void addwater (intvoid 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,
    • You do not need to define the body of a method
    • No need to describe the visibility of the method
Three: interface use?? UseImplementsKeyword Implementation interface? Grammar
    1. Class name implements interface name {}

Attention:

    1. the public method of a class forms an interface. Therefore, all methods that appear in the interface default to public.
    2. Interfaces cannot be instantiated, and sub-classes are required to implement interfaces when using interfaces

    3. The implementation of the interface? A class that implements an interface must implement all the methods within the interface, otherwise the class must be declared as an abstract class

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 MusiccupImplementsCup {Publicvoid addwater (int W) { Span style= "COLOR: #0000ff" >this.water = this.water + W; Span style= "COLOR: #0000ff" >public void Drinkwater (int< Span style= "COLOR: #000000" > W) {this.water = this.water-< Span style= "COLOR: #000000" > W; } public int Watercontent ( {return this.water;} Span style= "COLOR: #0000ff" >private int water = 0;}   

Four: The meaning of the separation interface

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.

Five: 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 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 MusiccupImplementsMusicplayer, cup{public void addwater (int< Span style= "COLOR: #000000" > W) {this.water = this.water +< Span style= "COLOR: #000000" > W; } public void Drinkwater ( Int W) {this.water =  This.water- W;} public void play () { System.out.println ("la...la...la" private int water = 0;}   

  

Six. The difference between abstract classes and interfaces

1) Abstract classes can provide implementation details for member methods, whereas methods in an interface can only be abstract methods, the default modifier public abstract?

2) member variables in an abstract class can be of various types, while member variables in an interface are constants only, default modifiers public static final
?

3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

4) A class can only inherit an abstract class, and the main purpose of the interface is to extend the function, there is no specific interface of the implementation of a multi-class, but the implementation of multiple interfaces.

Note:any class can have direct access to constants in the interface

Seven: summary

1, abstract class in the Java language represents an inheritance relationship, a subclass can only exist one parent class, but there may be more than one interface.

2, in the abstract class can have their own member variables and non-abstract class methods, but the interface can only have static immutable member data (but generally do not define member data in the interface), and all of its methods are abstract.

3, the abstract class and the interface reflects the design concept is different, the abstract class represents is "is-a" the relation, but the interface represents is "the like-a" the relation.

Abstract classes and interfaces are two different abstract concepts in the Java language, and their presence provides very good support for polymorphism, although there is a great similarity between them. But for their choices, it often reflects your understanding of the problem domain. Only a good understanding of the nature of the problem domain can make the correct and reasonable design.

Java Basics-Interface

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.