Java Basics (contd.) |
1. Interfaces An interface declares a set of methods and constants, without actually providing an implementation for any of those methods. A class is said to implement an interface if it provides definitions for all of the methods declared in the interface. Interfaces provide a way to prescribe the behavior that a class must have. In this sense, an interface bears some resemblance to an abstract class. An abstract class may contain default implementations for some of its methods; it is an incomplete class that must be specialized by subclassing. By constrast, an interface does not provide default implementations for any of the methods. It is just a way of specifying the functions that a class should contain. There is no notion of specialization through function overriding. Some points to note about interfaces: A class may implement more than one interface, whereas it can only extend one parent class. An interface is treated as a reference type. Interfaces provide a mechanism for callbacks, rather like pointers to functions in C++. An interface can extend another interface. Here is an example of using an interface. import java.util.*; interface Collection { class Stack implements Collection { // A last in first out (LIFO) process. public Stack() { // This adds an element to the top of the stack. // This removes an element from the top of the stack. // This prints out the stack in order from top to bottom. class Queue implements Collection { // A first in first out (FIFO) process. public Queue() { // This adds an element to the bottom of the queue. // This removes an element from the top of the queue. // This prints out the queue in order from top to bottom. class Main { // Create a stack and add some objects to it. The function CreateSomeObjects takes a // Remove an element from the stack and then print it out. // Create a queue and add some objects to it. // Remove an element from the queue and then print it out. // Create some objects and add them to a collection. Class Integer allows us to create integer What happens when a program encounters a run-time error? Should it exit immediately or should it try to recover? The behavior that is desired may vary depending on how serious the error is. A "file not found" error may not be a reason to terminate the program, whereas an "out of memory error" may. One way to keep track of errors is to return an error code from each function. Exceptions provide an alternative way to handle errors. The basic idea behind exceptions is as follows. Any method with the potential to produce a remediable error should declare the type of error that it can produce using the throws keyword. The basic remediable error type is class Exception, but one may be more specific about the type of exception that can be thrown e.g. IOException refers to an exception thrown during an input or output operation. When an exception occurs, we use the throw keyword to actually create the Exception object and exit the function. Code that has the potential to produce an exception should be placed within the try block of a try-catch statement. If the code succeeds, then control passes to the next statement following the try-catch statement. If the code within the try block fails, then the code within the catch block is executed. The following example illustrates this. class LetterTest { k = System.in.read(); return (char)k; public static void main(String[] args) { try { Note: in addition to the Exception class, Java® also provides an Error class, which is reserved for those kinds of problems that a reasonable program should not try to catch. |
地震让大伙知道:居安思危,才是生存之道。