Simple Problem for 2nd year Comp Sci Class

T

traviswinter

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
 
M

Manish Pandit

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
 
O

Oliver Wong

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. [...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,268
Messages
2,571,095
Members
48,773
Latest member
Kaybee

Latest Threads

Top