This tutorial tells two JavaTM programming language features to help you manage the relationships between classes. First, you will learn how to write and use the communication interface protocol between the objects. Then, you will learn how to bundle classes and interfaces to a package.
6.1 Creating an interface
In this tutorial, you will see an example of executing an interface, and you can read some content about the interface: What do they are for, why should we write an interface, how to write an interface, and so on.
Java programming language supports interfaces, you can use interfaces to define a protocol for your behavior. These behaviors can be performed in any class in the class hierarchical structure.
6.1.1 What is an interface?
This section defines what interfaces, gives an example of an interface and how to use it, and discuss why you need to use an interface in the program.
The interface defines a protocol for behavior that can be performed in any class in the class hierarchical structure. The interface defines a number of methods but does not perform them. Class Performing Interface Protocols to perform all methods defined in the interface, so suitable for certain behaviors.
Because the interface is a simple unformented series and some abstract methods, you may think about what is the difference between the interface on abstract classes. It is quite important to know their differences. There are the following differences between them:
The interface cannot perform any methods, and the abstract class can. Classs can perform many interfaces, but there is only one parent class. The interface is not part of the class classification structure. The class that does not contact can perform the same interface.
Let's take an example below.
6.1.1 What is an interface?
If you have written a class, this class can pay attention to the price of the stock. This class allows other classes to be registered to know what specific stock price changes. First, write the StockMonitor class, which can perform a method to register for other objects to get notifications.
Public class stockmonitor {
Public void WatchStock (stockwatcher watcher,
String Tickersymbol, Double Delta) {
...
}
}
The first parameter of this method is not STOCKWATCHER object. StockWatcher is the name of an interface, which will be given in the back tutorial. This interface declares a method: ValueChanged. Objects to be notified of shares must try an instance of classes of interfaces and valuechanged methods. The other two parameters provide stock symbols to observe the number of changes. When the StockMonitor class detects a change of interest, it calls Watcher's ValueChanged method.
WatchStock method To ensure that all registration objects perform a ValueChangeD method through the data type of the first parameter. If STOCKMonitor has used a class name as a data type, it is necessary to force its user's class relationship. Because classes can only have a parent class, this also limits what type of data can be used. By using the interface, the registration object class can be applet or thread, such as it allows any class in the class hierarchical structure to use this service.
6.1.2 Defining the interface
Define an interface to create a new category. Interface definitions require two components: interface definitions and interface entities.
InterfaceDeclaration {
InterfaceBody
}
InterfaceDeclaration declares a variety of properties about the interface, such as its name and whether other interfaces are extended. This interfacebody contains the constant and method declarations in the interface.
Two components are given by interface definitions as shown in Figure 29: Interface declarations and interface entities. The interface declaration defines a variety of properties about the interface, such as its name and whether other properties are extended; interface entities contain constants and methods for interfaces.
(Figure 29)
The STOCKWATCHER interface and interface definitions are:
Public interface stockwatcher {
Final String
Sunticker = "sunw";
Final string oracleticker = "orcl"; final string ciscoticker = "csco";
Void ValueChanged (String Tickersymbol, Double NewValue);
}
The interface defines three constants, they are the symbol of the stock market for Watchable stock. This interface also defines the ValueChanged method, but does not perform it. The class that executes this interface provides a method.
The following is a statement of the interface:
All possible components of the interface declaration are given in Figure 30:
(Figure 30)
Two elements are required in the interface definition: the name of the interface keyword and interface. Publicity indicates that the interface can be used in any class in any package. If you don't specify an interface as public, then the interface can only be used in the package of the defined interface.
Interface definitions can have another component: SuperInterfaces series. An interface can extend additional interfaces, which can be expanded. However, classes can only extend an additional class, and the interface can extend any interface. Superinterfaces series of peppers are separated by commas, which can be expanded from new interfaces.
At this time, you may ask: How do you say that the interface entity? Don't worry, here will explain:
The interface entity contains the method declarations for all methods included in the interface. The method declaration in the interface can keep up with a comma because the interface does not provide execution for the method defined above it. All methods defined in the interface can imply Public and Abstact.
The interface can include a constant S declaration and method declaration. All constants defined in the interface can be public, static, and final. Defining member declarations in an interface is not allowed to use some statement modifications, such as those you cannot use Transient, Volatile or Synchronized in the member declarations in the interface. Also you can't use private and protected modifiers when you declare the interface.
6.1.3 Execution Interface
In order to use the interface, you have to write a class that execute the interface. If a class can perform an interface, this class provides a method of performing all the methods defined in the interface.
An interface defines the protocol of a behavior. A class can perform an interface according to the protocol defined in the interface. In order to declare a class to execute an interface, you must include an execution statement in the statement of the class. Your class can execute multiple interfaces (because the Java platform supports multiple inherits of the interface), so you can list the interface series executed by the class later, which are separated by commas.
The following is some example of an applet, which performs the stockwatcher interface:
Public class stockapplet extends applet imports stockwatcher {
...
Public void valuechanged (String Tickersymbol, Double newValue) {
IF (Tickersymbol.Equals (SUNTICKER)) {
...
} else if (tickersymbol.equals (oracleticker) {
...
} else if (tickersymbol.equals (ciscoticker) {
...
}
}
}
Note here, this class references the constant defined in the stockwatcher.sunticker, such as ORACLETICKER, and more. The class that performs the interface inherits the constant defined in the interface. So these classes can use simple names to reference constants. You can use the following statement to make any other class using interface constants:
StockWatcher.Sunticker
In essence, when the class is executed, a contract is signed. All classes must perform all methods defined in the interface and its superInterfaces, and classes must be defined as Abstract. The signature of this method (the name, and the number of parameter types in the class) must match the method's signature. The STOCKAPPLET performs the SockWatcher interface, so the applet provides the ValueChanged method. This method discloses the display of Applets or uses this information. 6.1.4 As a type of interface
When you define a new interface, from essentially, you define a new reference data type. You can use the interface name (such as variable declaration, method parameters, etc.) to use the interface name. Recall, we have the data type of the first parameter in the WatchStock method in the StockMonitor class, for stockwatcher:
Public class stockmonitor {
Public void WatchStock (stockwatcher watcher,
String Tickersymbol, Double Delta) {
...
}
}
Only an instance of a class that executes an interface can assign a reference variable, which is the interface name. Therefore, only instances of the class that execute the STOCKWATCHER interface can be registered to obtain a notification of the stock value change.
6.1.5 Interface cannot be developed
If you pass the interface to other programmers, the interface has a limit you should pay attention: the interface cannot develop. The following explanation:
If you want a function to StockWatcher. For example, you want to add a way to report the current stock price, regardless of whether the value is changed:
Public interface stockwatcher {
Final String Sunticker = "sunw";
Final string oracleticker = "orcl";
Final String Ciscoticker = "CSCO";
Void ValueChanged (String Tickersymbol, Double NewValue);
Void CurrentValue (String Tickersymbol, Double NewValue);
}
However, if you do this change, all classes of the old version of the Stockwatcher interface will be interrupted because they cannot perform this interface. The interface cannot be developed, this is all programmers to know. To reach the above to add a method of adding a method you can create more interfaces. For example, you can create one
Subinterface (sub-interface) StockTracker ::
Public interface stockwatcher extends stockwatcher {
Void CurrentValue (String Tickersymbol, Double NewValue);
}
6.2 Creating and Using Package
This section will describe how to bundle your class into the package, and teach you how to use the classes in the package:
In order to make the class more easily discovered and used, and to avoid name conflicts, control access, programmers to bundle relevant classes and interfaces into the package. Classs and interfaces are part of the Java platform, which are members of various packages that are bundled by functions: Basic classes are in java.lang, and classes for reading and writing in java.io, etc.. You can put your class and interface to the package:
Let's take a look at the following classes and check why you want to place them into the package. You can write a range of graphics objects: such as Circles, Rectangles, Lines, and Points, you can also write interface Draggable, which can move these graphics objects when the user drags the mouse: // In Graphics.java file
Public Abstract Class graphic {
.
}
// Document in Circle.java
Public Class Circle Extends Graphic Implements Draggable {
.
}
// File in Rectangle.java
Public Class Rectangle Extends Graphic IMPLEments Draggable {
.
}
// Document in Draggable.java
Public interface draggable {
.
}
Then why do you have to bundle these classes and interfaces into a package? The reasons are as follows:
All programmers can easily decide which classes and interfaces are related. All programmers know where to find classes and interfaces with graphic related functions. The name of the class will not conflict with the names in other packages because each package creates a new namespace. You can allow access to unrestricted access in the package, as well as restricted access outside the package.