Extending the Enumeration interface

J

Joseph Dionne

Is there a way to extend the Enumeration interface, or any interface for
that matter, so that a declared method can throw additional Exceptions?

I want to maintain a Vector of regular Enumerations and the extended
Enumerations that can throw other exceptions besides NoSuchElementException.
 
R

Roedy Green

Is there a way to extend the Enumeration interface, or any interface for
that matter, so that a declared method can throw additional Exceptions?

If you add extra exceptions not part of the interface contract,, the
callers of your methods are not prepared for them. They would have to
either catch or pass them on up.


What you would have to do is put some very general exception in your
interface contract. Then you could effectively add stuff later.
 
J

Joseph Dionne

Roedy said:
If you add extra exceptions not part of the interface contract,, the
callers of your methods are not prepared for them. They would have to
either catch or pass them on up.


What you would have to do is put some very general exception in your
interface contract. Then you could effectively add stuff later.

But I cannot even extend the Enumeration interface be adding a new
parameter, i.e. nextMethod(boolean b), to achieve my affect. And I was
unaware that the throws was part of a method signature. Using your rule
of thumb, one should not be able to extend a class where a method throws
additional exceptions.
 
X

xarax

Joseph Dionne said:
But I cannot even extend the Enumeration interface be adding a new
parameter, i.e. nextMethod(boolean b), to achieve my affect. And I was
unaware that the throws was part of a method signature. Using your rule
of thumb, one should not be able to extend a class where a method throws
additional exceptions.

You cannot change the signature of a method in a subclass
or sub-interface. If you change the parameters, then you
are just overloading the name. Changing the "throws" clause
to add a new checked exception is not permitted, because
existing clients of the method are not prepared to handle
the new exception.

If you add a new method, either by a new name or a new
signature that cannot be confused with an existing
method signature in the interface hierarchy, then you
can specify whatever you need in that method's "throws"
clause. However, clients must refer to the instance
type using the sub-interface type in order to see the
new method at compile time.

Thus, you cannot change the Enumeration methods in
a sub-interface. You cannot use a sub-interface to
specify an alternative method that throws your desired
exception, and use that sub-interface in an implementing
instance as an "Enumeration" reference type. You must
use the sub-interface type at compile time.
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top