Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX:about interface design

 
Nai chao Wu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone
My English is poor,I hope that everybody can understand me.
sun provides a interface DBMain :
public interface DBMain {
// Reads a record from the file. Returns an array where each
// element is a record value.
public String [] read(int recNo) throws RecordNotFoundException;
// Modifies the fields of a record. The new value for field n
// appears in data[n].
public void update(int recNo, String [] data)
throws RecordNotFoundException;
.
.
.
These methods only throws a Exception-RecordNotFoundException
I want to design a remoteInterface which extends DBMain and Remote
These methods in the remoteInterface must throw RemoteException.
If these method throw RemoteException,program won't compile successful.
Because method in the DBMain don't throw RemoteException or its parent.
Can interface DBMain be modify to throw IOException?
who have a good solution?
Thanks
 
Paul Tongyoo
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nai chao Wu:

Can interface DBMain be modify to throw IOException?
who have a good solution?

Hi Nai--
Forgive me if my advice is incorrect -- it's been awhile since i've posted after I passed the SCJD exam, I needed a break!
You can definitely extend the DBMain interface to adhere to the one you want. Just make sure you don't change the actual DBMain interface itself.

However, there is an alternate approach you may want to consider: rather than extending the class and inheriting all of its functions, have you considered using some sort of object delegation approach?

In both cases I'm adding to the functionality of DBMain, but make sure I don't change the DBMain interface itself.
HTH
Paul
[ April 03, 2004: Message edited by: Paul Tongyoo ]
 
Nai chao Wu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul
My meaning is The exceptions thrown by a subclass'method may be equal to or fewer than those thrown in the superclass'method.The overridden method must explicity declare which methods it can throw.
For example
public interface DBMain{
public String [] read(int recNo) throws RecordNotFoundException;
}
import java.rmi.RemoteException;
import java.rmi.Remote;
public interface RemoteInf extends DBMain ,Remote
{
public String [] read(int recNo) throws RecordNotFoundException,RemoteException;
}
compile:read(int) in RemoteInf clashes with read(int) in DBMain;
overridden method does not throw java.rmi.RemoteException
If method in DBMain throw RemoteException or IOException,it will compile successful.
but DBMain isn't modified.

Nai
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic