Java programmer must read: Basic (8)

Source: Internet
Author: User
Java programmer must read: basic articles
Time: 2001/09/13 13:31 Author: ZSC Pacific Network College
 
2.6 What is an interface
An interface is a contract that collects methods and constant forms. When a class executes an interface, it promises to declare that all methods are executed in that interface.
An interface is a device or a system that is an unrelated entity for interaction. According to this definition, remote control is an interface between you and the TV, while English is the interface between two people, and the behavior agreement that enforces in the military is the interface between different equivalence persons. In the Java language, an interface is a device that is used to interact with other objects. An interface may be similar to a protocol. In fact, other object-oriented languages have interface functionality, but they invoke their interface protocols.
The bike class and its class hierarchy define what a bicycle is. But bicycles interact with the real world in other ways, for example, in warehouses where bicycles can be managed by an inventory process. An inventory procedure does not care what kind of management project as long as the project provides a certain information, such as price and tracking number. Instead of forcing the class to relate to other unrelated items, the inventory procedure establishes a communication protocol. This protocol consists of constants and method definitions contained in an interface. This inventory interface will define (but not execute) methods to set up and get retail prices, specify tracking numbers, and so on.
In order to operate in the inventory procedure, the bicycle class must comply with this protocol when executing the interface. When one executes an interface, the class adheres to all the methods defined in the interface. As a result, bicycles provide execution for these settings and to obtain retail prices and to specify tracking values, and so on.
You can use interfaces to define a protocol for a behavior that can be executed by any class in the class hierarchy. The main benefits of the interface are a few things:
It is not necessary to force class relationships to intercept similar places in unrelated classes.
A method that declares one or more classes that you want to execute.
Exposes the object's programming interface without exposing the object's class.
2.7 How to translate these object-oriented concepts into code
This section will show you the code that creates objects, executes classes, sends messages, creates a parent class, and executes an interface.
Here is an applet (an applet is a program written in the Java programming language) that can run on a Web browser that is compatible with the Java platform, such as HotJava or Netscape Navigator, called ClickMe. As shown in Figure 10, when you click anywhere in the box, a red dot appears.
(Figure 10)
Tip: The applet above needs to be JDK1.1. If you use an older browser that does not support JDK1.1, you will not be able to run this applet. Instead, you need to look at this page in a 1.1 browser, such as HotJava, JDK Applect Browser (appletviewer), or a version of Netscape Navigator and Internet Explorer.

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.