wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Interface reuse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Interface reuse" Watch "Interface reuse" New topic
Author

Interface reuse

Stan Griffith
Greenhorn

Joined: Jun 04, 2005
Posts: 11
Hi All,

I am doing the URLyBird project.

I have the following high level interface which encapsulates the lower level Data class functionality.



I have a remote interface which re-uses the above interfces.



However, when I implement the remote class and add the RemoteException in the method signature, I get a compilation error.



But I have seen similar reuse of existing interface in Max's book. Am I missing something?

I know that I can define two different interfaces for local and remote invocations which may not be an optimal design.
I am really having a hard time figuring this out. Any help is appreciated.

regards

Stan

[Andrew: Forced line breaks in source code so that horizontal scrolling should not be required].
[ June 05, 2005: Message edited by: Andrew Monkhouse ]
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Is that the actual code you are trying to compile? Your reserve() method has a ";" instead of "{". The compiler wouldn't like that.
Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
If it isn't just a typo like John suggests, what is your actual compilation error?

Michal
Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86

I think the problem may be that the implementation of the reserve() method above throws a RemoteExeption - when in the interface IDBAdapter, only ReservationFailedException and InvalidDataFileException are thrown. If in IDBAdapter you define reserve() to throw a RemoteException (or a superclass of RemoteException, for example IOException, which is what I think I will be doing in my similar situation), this would solve the problem.

The reason is that when you implement an interface, you cannot throw new checked exceptions that were not defined in the original interface - unless they are a subclass of one of the originally defined exceptions (I think so anyway!).

Michal

[Andrew: added line breaks to the code so that horizontal scrolling is not required]
[ June 05, 2005: Message edited by: Andrew Monkhouse ]
Stan Griffith
Greenhorn

Joined: Jun 04, 2005
Posts: 11
Hi Michal,

You are right. It is a typo. You are also right about introduction of a checked exception. My problem is this.

I have an interface which I use when I make local connections (alone as paramater)


and another interface which is a remote interface that extends the above interface as well as 'Remote' interface. I use this in the code when I have to make networked conections.



This interface has no methods but uses the methods in 'IDBAdapter' interface.
So this interface by definition has to throw Remote Exception in the method signatures.

But when I do so, I get the compilation error. So all I am trying to do is figure out a way to reuse my existing interface to make the design clean.

How are you dealing with this problem? Do you have two Adapter interfaces for Remote and local connections?

Also, in Max's book there's an example which does this interface reuse and it compiles perfectly too.

regards

Stan.
Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
Originally posted by Stan Griffith:

Also, in Max's book there's an example which does this interface reuse and it compiles perfectly too.

In Max's book he has the DBClient interface, which, as I understand it, corresponds to your IDBAdapter interface. Each of the methods in the DBClient interface (p173-p174) is declared to throw either an IOException, or an Exception. Note that RemoteException is a subclass of IOException (and obviously Exception). Thus any implementation of DBClient can validly throw a RemoteException for any of it's DBClient methods - since a RemoteException is an IOException.

Your interface IDBAdapter, as I understand it, declares that it's methods throw ReservationFailedException and InvalidDataFileException, which are not superclasses of RemoteException. Therefore any class that implements them cannot throw a RemoteException. As you have seen, an attempt to do so results in a compilation error.
Originally posted by Stan Griffith:
So all I am trying to do is figure out a way to reuse my existing interface to make the design clean.
How are you dealing with this problem? Do you have two Adapter interfaces for Remote and local connections?

My DBClient class will be as Max's - everything throwing an IOException. I think this is fine, as all the methods relate to accessing a database, so IOException seems perfectly natural.

I'm not sure if you could have a second interface, so have one for remote and one for local connections, and still have a "clean" desgin. For one thing, your GUI/local client should somehow interact with the database, be it local or remote. I think that for the most part it shouldn't really have to worry about what type of connection it is dealing with, and so a single common interface, like DBClient, should be used.

To sum up, I don't think you can reuse your interface without changing it, and keep a clean design.

I hope this helps.

Michal.

Edit: changed from "I'm not sure if" to "I don't think" in last line.
[ June 05, 2005: Message edited by: Michal Charemza ]
Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
I think another reason that it would be difficult to use your existing interface, and a second "remote" interface, is more related to the server side. A class couldn't implement both the remote and local interfaces, due to the fact that one would throw RemoteException in its methods and the other wouldn't. Therefore (I think) the DBAdapter class (as in Max's book) would have to be wrapped in another wrapper that implemented the remote interface.

Come to think of it, for front-end design, a similar thing could be done on the client. i.e. a remote interface object would be received, but then wrapped in a localinterface object for the front-end to interact with, or something similar.

However, this all seems a bit overly complicated, and not clean, to me.

Michal.
Stan Griffith
Greenhorn

Joined: Jun 04, 2005
Posts: 11
Hi Michal,

Thanks for the clarification. I am also not getting the compile error anymore. I agree with your explanation.

regards

Stan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface reuse
 
Similar Threads
RMI pass by value or by reference ?
UrlyBird RMI Interface
FBN : Exception in High level Data Interface
General design question
NX: URLy Bird 1.3.1 Connection Factory Design