I have code like this class parent { void print()throws IOException{} }class sub{ void print() throws Exception{}}It will not allow like this super class method should throw an exception of sub class Exception which is in sub class method?

Showing Answers 1 - 5 of 5 Answers

seyad

  • Jun 20th, 2006
 

Hi,class parent {void print()throws IOException{}}class sub extends parent{void print() throws Exception{}}When you are overriding a method, you can throw the same exception or subclass of that exception(parent class method throws), not any exception....the following is correctclass parent {void print()throws Exception{}}class sub extends parent{void print() throws IOException{}}

  Was this answer useful?  Yes

mahesh

  • Jun 20th, 2006
 

hi, naresh

                the above is true ----here void print() method is overriding method in super and sub classes. here we are not decreasing the exception hierarchy in sub class we increasing no problem because we should not decrease the hierarchy of the exception in the sub class

              so the above code is true ......IOException is a subclass of Exception

  Was this answer useful?  Yes

amit

  • Jan 5th, 2007
 

IN case of overriden , method  of subclass can only throw all or subset of  Exceptin Class Defind in the super class

  Was this answer useful?  Yes

anil kumar gupta

  • Mar 10th, 2007
 

/*According to your prgram i have compile but still it is giving error can u pls tell me the reason*/


class sub
     {
        void print()throw Exception
            {
                System.out.println("overriding");
            }
     }
    



class child1 extends sub{
void print()throw IOException
    {
            System.out.println("overriding");
    }
public static void main(String args[]){


}
}

  Was this answer useful?  Yes

Rudresh

  • Sep 19th, 2007
 

class parent
{
void print() throws IOException{}

}

class sub
{
void print() throws Exception{}

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions