• Post Reply Bookmark Topic Watch Topic
  • New Topic

"Missing" throws-clause in implementation of remote service (RMI)  RSS feed

 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm referring to page 619 of Head First Java, chapter about RMI.

I don't get this:

The remote service implements the interface MyRemote.

A method of MyRemote goes like this:

public String sayHi() throws RemoteException;


But now in the implementing service the throws clause is missing:

public String sayHi(){...}

How can that be?

I thought that if you implement an interface the signature of the method must be adopted as it is... and that includes throws-clauses as well... am I wrong?

Thanks in advance!
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't add new checked exceptions to method definition while implementing an interface.
But you can skip exceptions (class B) or make them more specific (class C). You can also add non-checked exceptions (class D).
Note: ClassNotFoundException is a subclass of ReflectiveOperationException which is a checked exception.

This compiles and works.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thanks for the answer.

I have some remarks:

1) YOU CAN LEAVE OUT AN EXCEPTION??? I wonder what did the Sun engineers have in mind when writing the compiler?

2) I'm almost finished with the Head First Java book... unfortunately there is nothing in this book that handles this... can you give me a source from where I can learn this / from where you learned this?

Thanks
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can't leave out an exception. When implementing an interface you must stick to the contract of every method.

The contract is:
When you declare any checked exception (say E) in a method definition via throws keyword that means two things:
- This method might throw E or any excaption that extends E.
- This method can't throw any other checked exception.

So it is perfectly legal to create a class implementing an interface where some or all exceptions are skipped.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats all new to me... alas it doesn't say these things in HFJ book... :/

I wonder if I have learned Java from the wrong book?

Can you please tell me a source (maybe a book ...) where you learned this from and where I can learn and read about these things?

It sucks... I almost through 700 pages and still some vital infos lacking here and there ...
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is said on page 619:
Head First Java wrote:You have to implement all the interface methods, of course. But notice that you do NOT have to declare the RemoteException.


Don't be frustrated. Head First Java is meant for beginners. It does not cover all topics about Java. You can't expect to know everything about Java after reading it.
Even in the book there is an appendix called The Top Ten Topics that almost made it into the Real Book... (beginning at page 659)
This suggest that there is plenty of learning and new topics to discover .

And this is my 500th post ;)
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but is hfj sufficient for the ocp 6 exam? or do I need to read another, more detailed book as well?

can i just start with the scjp6 study guide from sierra/bates after finishing hfj (is the lacking/remaining info provided there)?

[oca is only prerequisite for ocp7, not for ocp6.. right?]

thanks alot.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations on the 500 PP.

There are various books I would suggest; once you are a bit more experienced I would recommend Horstmann and Cornell. There are all sorts of other resources; you can find one I learned most from here.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, congrats! )

and thanks everyone...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!