Hello everyone, looking at some of the other threads on the group this
should be cake :)
I'm doing an assignment (programming in Java) which implements a Heap
based Priority Queue. Basically just trying to get it up and running.
The problem exists like this:
ArrayListCompleteBinaryTree implements the interface CompleteBinaryTree
which implements the interface Tree. Tree has a replace method that
does not throw an exception. The replace method in
ArrayListCompleteBinaryTree can throw an exception due to the way it's
implemented. I would change the replace method in Tree but if I do so
another class, HeapPriorityQueue, which has an
ArrayListCompleteBinaryTree object in it's methods must also throw the
exception for the replace method.
Is there any way around this? I'd figure that since
ArrayListCompleteBinaryTree already covers the exception, that
HeapPriorityQueue would already be covered. Am I wrong? Also, I
should mention that the code I'm using is direct out of our courses'
text (as we were told to use) but it is missing certain methods
(replace is actually given) and does not compile right out of the book.
This things due in a couple of days so I'd appreciate any help anyone
could give me. Thanks!
Travis
Manish Pandit - 19 Oct 2006 01:56 GMT
If the interface does not declare exceptions, the implementation cannot
throw any checked exceptions. If it does, then the implementation can
declare the same checked exception or any of its subclasses. If your
implementation is causing the method to throw an exception, you have to
handle it within the method, or revisit the design as this indicates a
flaw in the design. Changing the interface signature just because an
implementation needs to throw an exception is not a good idea either.
One more thing - what do you mean by class XYZ already *covers* an
exception?
-cheers,
Manish
Oliver Wong - 19 Oct 2006 21:59 GMT
> ArrayListCompleteBinaryTree implements the interface CompleteBinaryTree
> which implements the interface Tree. Tree has a replace method that
[quoted text clipped - 4 lines]
> ArrayListCompleteBinaryTree object in it's methods must also throw the
> exception for the replace method.
[...]
> Am I wrong?
I think you're wrong. You can have Tree's replace declare itself as
throwing an exception, without having HeapPriorityQueue declare itself to
throw an exception.
Alternatively, you could have HeapPriorityQueue go ahead and declare as
throwing an exception, but never actually throw the exception in the
implementation.
- Oliver