• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface reuse

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that the actual code you are trying to compile? Your reserve() method has a ";" instead of "{". The compiler wouldn't like that.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it isn't just a typo like John suggests, what is your actual compilation error?

Michal
 
Michal Charemza
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michal,

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

regards

Stan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic