GeekInterview.com
Series: Subject: Topic:
Question: 22 of 573

Exception

When super class method throws exception then subclass also throws the same Exception, which will compile?
Asked by: sunithamule | Member Since Dec-2011 | Asked on: Dec 2nd, 2011

View all questions by sunithamule

Showing Answers 1 - 4 of 4 Answers
dev

Answered On : Dec 11th, 2011

No, When super class method throws exception then subclass does not throws the same Exception

Yes  1 User has rated as useful.
  
Login to rate this answer.

Yes, we can

Code
  1. package sample;
  2.  
  3.  class Super {
  4.  
  5.        
  6.                 void foo() throws NullPointerException
  7.                 { System.out.println("Super");
  8.                 }
  9.                 }
  10.                 class SupSub extends Super {
  11.                 void foo()throws NullPointerException
  12.                 {
  13.                         System.out.println("SupSub");
  14.                 }
  15.                 }
  16.         public  class super123{
  17.                
  18.                 public static void main( String[] argv ) {
  19.                         SupSub sup=new SupSub();
  20.                         Super spr=new SupSub();
  21.                         spr.foo(); // Output SupSub }
  22.                 }
  23.  
  24. }
  25.  

  
Login to rate this answer.
alin20_84

Answered On : Feb 14th, 2012

View all answers by alin20_84

Here foo can skip adding throws NullPointerException because is an unchecked exception
class SupSub extends Super {

void foo()throws NullPointerException

{

System.out.println("SupSub");

}

}

  
Login to rate this answer.
sampra

Answered On : Mar 6th, 2012

View all answers by sampra

Yes we can ..sub class can throw same or subclass of that exception

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.