Site hosted by Angelfire.com: Build your free website today!
 

Interface Example

public interface StockServer {
    final static int SOME_CONSTANT = 9999;
    StockServer();
    void addClient(StockClient client);
    void removeClient(StockClient client);
    ...
}

The StockServer interface defines a constant, SOME_CONSTANT, a constructor and two methods. This represents a contract/API of classes that implements the StockServer interface.

public interface StockClient {
    void updateChange(String tickerSymbol, double newValue);
}

The StockClient interface represent clients of the StockServer implementation classes. Together, the StockServer and the StockClient allows any StockClient implementation classes to work with any StockServer implementation.

class StockQuoteApplication implements StockClient {
    void updateChange(String tickerSymbol, double newValue) {
        ....   // Display the new information
    }
}
class StockTraderApplication implements StockClient {
    void updateChange(String tickerSymbol, double newValue) {
        ....   // Place sell or buy order
    }
}