Extending the Enumeration interface

Discussion in 'Java' started by Joseph Dionne, Jun 11, 2004.

  1. 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.
    Joseph Dionne, Jun 11, 2004
    1. Advertisements

  2. Joseph Dionne

    Roedy Green Guest

    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.
    Roedy Green, Jun 11, 2004
    1. Advertisements

  3. 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.
    Joseph Dionne, Jun 11, 2004
  4. Joseph Dionne

    xarax Guest

    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.
    xarax, Jun 12, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.