Stephen McConnell wrote:

Leo:

The error occurred when creating a class that extended
ExcaliburComponentManager - the derived class contained implementations of
the initalize or contextualize methods corresponding to the Initializable
and Contextualizable interfaces.  The compiler complained because my new
class was declaring the exceptions (I was including the respective throws
clause).  So, to be absolute correct - the compilation error was on my
class - not ExcaliburComponentManager.  However, the compilation error
arrived because ExcaliburComponentManager implementation doesn't declare the
respective exceptions.

What is interesting here is that ExcaliburComponentManager implements the
Initializable and Composable interfaces without declaring the required
exceptions and yet this compiles ok (which I confess I don't understand).

This has to do with inheritance. Java allows you to hide exceptions for

inherited methods, but you cannot add them.  This is because the super class
implementation declares the set of exceptions that it will allow.  You can
get more specific--but you cannot go outside that set.  Once you eliminate
the exception in the inheritance hierarchy, you have a super class that has
a smaller set than the base class.  Anything that extends this super class
now must repect *it's* declared set of exceptions.

The comments in the following heirarchy of classes will help you understand.
The question is, is there a *reason* you want to throw an exception from the
subclasses of ExcaliburComponentManager or are you declaring it to be
semantically correct?

class BaseClass
{
    /**
     * Specifies that it *can* throw an exception.  It does not mean that
     * it *has* to.
     */
    abstract void method() throws Exception;
}

class SuperClass extends BaseClass
{
    /**
     * This is legal because it is more specific, and does not violate the
     * contract specified by BaseClass.
     */
    void method() throws IOException;
}

class IllegalClass extends SuperClass

{
   /**
    * This is not legal because it violates the contract specified in SuperClass
    */
   void method() throws SocketException;
}

class NoExceptionClass extends SuperClass

{
    /**
     * This is legal because exceptions do not need to be thrown, but now no
     * other exceptions are allowed to be thrown.
     */
    void method();
}

class SecondIllegalClass extends NoExceptionClass

{
     /**
      * This is not legal because the super class narrowed the contract on
      * thrown exceptions to none.  If a class was cast as the NoExceptionClass,
      * then tried to throw an exception, it would violate the NoExceptionClass
      * contract.
      */
     void method() throws Exception;
}



--

"They that give up essential liberty to obtain a little temporary safety
 deserve neither liberty nor safety."
                - Benjamin Franklin


-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>



Reply via email to