站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java 2 SDK v1.4.2, Java 2 SDK 英文文档

Java Remote Method Invocation: 2 - Distributed Object Model - Java 2 SDK v1.4.2, Java 2 SDK 英文文档

CONTENTS | PREV | NEXT Java Remote Method Invocation


2.5 Implementing a Remote Interface

The general rules for a class that implements a remote interface are as follows:

  • The class usually extends java.rmi.server.UnicastRemoteObject, thereby inheriting the remote behavior provided by the classes java.rmi.server.RemoteObject and java.rmi.server.RemoteServer.
  • The class can implement any number of remote interfaces.
  • The class can extend another remote implementation class.
  • The class can define methods that do not appear in the remote interface, but those methods can only be used locally and are not available remotely.
For example, the following class BankAcctImpl implements the BankAccount remote interface and extends the java.rmi.server.UnicastRemoteObject class:

package mypackage;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class BankAccountImpl
	extends UnicastRemoteObject
	implements BankAccount
{
	private float balance = 0.0;

	public BankAccountImpl(float initialBalance) 
		throws RemoteException 
	{
		balance = initialBalance;
	}
	public void deposit(float amount) throws RemoteException {
		...
	}
	public void withdraw(float amount) throws OverdrawnException,
		RemoteException {
		...
	}
	public float getBalance() throws RemoteException {
		...
	}
}


Note that if necessary, a class that implements a remote interface can extend some other class besides java.rmi.server.UnicastRemoteObject. However, the implementation class must then assume the responsibility for exporting the object (taken care of by the UnicastRemoteObject constructor) and for implementing (if needed) the correct remote semantics of the hashCode, equals, and toString methods inherited from the java.lang.Object class.



CONTENTS | PREV | NEXT
Copyright © 1997-2003 Sun Microsystems, Inc. All Rights Reserved.