10 Replies Latest reply: Aug 19, 2010 5:20 PM by 843789 RSS

    exception in super and sub class

    843789
      when super class method throws an exception then sub class overridden method can throws child exception but when we talk about constructor why we throws parent class exception in derived class constructor????
        • 1. Re: exception in super and sub class
          796262
          tcbhardwaj wrote:
          when super class method throws an exception then sub class overridden method can throws child exception but when we talk about constructor why we throws parent class exception in derived class constructor????
          Because every constructor makes an implicit call to the parent class's constructor.
          • 2. Re: exception in super and sub class
            843789
            bcuz parent is always called first in child.

            [u learn now|http://download.oracle.com/javase/tutorial/java/index.html]
            • 3. Re: exception in super and sub class
              796262
              Prepare to have your mind blown. What do you expect the output to be?
              public class ConstructorTest {
              
                   static class ParentClass{
                        public ParentClass(){
                             System.out.println("In parent constructor.");
                        }
                   }
                   static class ChildClass extends ParentClass{
                        public ChildClass(){
                             System.out.println("In child constructor.");
                        }
                   }
                   public static void main(String... args){
                        new ChildClass();
                   }
              }
              Run it.
              • 4. This Thread is now moved
                DarrylBurke
                Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
                • 5. Re: exception in super and sub class
                  843789
                  class P
                  {
                       p() throws Exception
                       {
                       }
                       void ok() throws RunTimeException
                       {
                       }
                  }

                  class C extends P
                  {
                       C() throws Throwable // why here parent of Exception class
                       {
                       }

                       void ok() throws ArithmeticException // why here child of RunTimeException class
                       {
                       }
                  }

                  Edited by: tcbhardwaj on Aug 19, 2010 11:54 AM
                  • 6. Re: exception in super and sub class
                    800331
                    tcbhardwaj wrote:
                    class P
                    {
                         p() throws Exception
                         {
                         }
                         void ok() throws RunTimeException
                         {
                         }
                    }

                    class C extends P
                    {
                         C() throws Throwable // why here parent of Exception class
                         {
                         }

                         void ok() throws ArithmeticException // why here child of RunTimeException class
                         {
                         }
                    }

                    Edited by: tcbhardwaj on Aug 19, 2010 11:54 AM
                    You can override ok() to where it doesn't throw anything too if you want. The throws clause doesn't count as part of the signature when overriding, only the name, arg list, and return type matter for that.
                    • 7. Re: exception in super and sub class
                      843789
                      kevinaworkman wrote:
                      Prepare to have your mind blown. What do you expect the output to be?
                      It did the obvious. Or did I completly miss the point?
                      • 8. Re: exception in super and sub class
                        796262
                        johndjr wrote:
                        kevinaworkman wrote:
                        Prepare to have your mind blown. What do you expect the output to be?
                        It did the obvious. Or did I completly miss the point?
                        I think you got the point from the start. It was my thinking that the OP didn't understand that child class constructors call parent class constructors. My example was supposed to show that to him. You already knew that's what constructors did, so your mind remains unblown.
                        • 9. Re: exception in super and sub class
                          3004
                          tcbhardwaj wrote:
                               C() throws Throwable // why here parent of Exception class
                               void ok() throws ArithmeticException // why here child of RunTimeException class
                          First, you never need to declare a throws clause for unchecked exceptions--RuntimeException, Error, and their descendants.

                          However, there are a couple of general rules for checked exceptions (such as IOException, SQLException, etc.). As you're studying these, keep in mind that a throws clause is a contract that promises what the method will not do. In particular, it doesn't promise that the method will throw those exceptions. It promises that it will not throw any other checked exceptions not covered by that throws clause.

                          1. A child method that overrides a parent method cannot throw any checked exception outside what's covered by the parent's throws clause. So if the parent method declared throws IOException, the child cannot declare throws Exception, or throws SQLException, because both of those are outside the IOException "sub-hierarchy."

                          2. A child method that overrides a parent method can declare to throw less than the parent throws. This is because, if the parent promises not to throw outside of some set, and the child promises not throw outside of a subset of that set, then clearly he's also not throwing outside of the parent's set. If the parent method declares throws SQLException, IOException, then the child can declare no exceptions at all, or just SQLException, or just IOException, or any combination of any of the subclasses of SQLE and IOE.

                          Rules 1 and 2 do not apply in the case of constructors, because no constructor can ever override another constructor.

                          3. When any method or constructor, A, invokes any other method or constructor, B, then any checked exception declared to be thrown by B must be either caught or declared to be thrown by A. If A catches or throws a parent of an exception thrown by B, then he's also implicitly catching or throwing the one that B mentions.
                          • 10. Re: exception in super and sub class
                            843789
                            kevinaworkman wrote:
                            johndjr wrote:
                            kevinaworkman wrote:
                            Prepare to have your mind blown. What do you expect the output to be?
                            It did the obvious. Or did I completly miss the point?
                            I think you got the point from the start. It was my thinking that the OP didn't understand that child class constructors call parent class constructors. My example was supposed to show that to him. You already knew that's what constructors did, so your mind remains unblown.
                            Oh. Nevermind.