The java interface extends the problem

I have to implement an RMI server, which will be the front end of the other two RMI services Therefore, I think it is logical to use the interface of this implementation for the interfaces of the other two services

public interface FrontEndServer extends Remote,BookServer,StudentServer
{
    // Block empty so far
}

But there is a method on studentserver

/**
 * Allows a student to borrow a book
 * 
 * @param studentID of the student who wishes to borrow a book
 * @param bookID of the book the student wishes to borrow
 * @throws remoteexception
 * @throws StudentNotFoundException when a student is not found in the system
 */
void addBookToStudent(int studentID,int bookID) throws remoteexception,StudentNotFoundException;

I want frontendserver to throw a booknotfoundexception too, because the service will also verify that the book actually exists before trying to add details

Is this possible or is it completely closed to my design concept, which is actually a bad design concept, as if other interfaces have changed? Would I be better able to write method signatures for all methods in frontendserver?

Solution

If you extend the interface (and if you implement it, the same applies), you cannot override the method and make it throw more checked exceptions than the original method You can throw away the same or less, but not more

Think about it:

interface A {
  void foo();
}

interface B extends A {
  void foo() throws IOException;
}

A a = new B() { ... }
a.foo();

IOException may be thrown, but you can't know That's why you can't do that

This is, of course, entirely acceptable:

interface A {
  void foo() throws IOException;
}

interface B extends A {
  void foo();
}

A a = new B() { ... }
try {
    a.foo();
} catch (IOException e) {
    // must catch even though B.foo() won't throw one
}

However, booknotfoundexception may extend runtimeException or RemoteException But I'm not sure it's a good way

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>