• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Overriding with Exception

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Select two methods that correctly override the following method? (Assume that all necessary packages have been imported).

protected int aMethod(int i) throws IOException {...}


A. int aMethod(int i) throws IOException{...}
B. protected int aMethod(int i) throws FileNotFoundException{...}
C. public int aMethod(int i) throws Exception{...}
D. public int aMethod(byte i) throws IOException{...}
E. protected int aMethod(int i) throws IOException,FileNotFoundException{...}

Plz some one explain whats the answer with clean explanantion
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is B and E.

Rules for override
1. method signature should be the same
2. argument list should be the same
3. return type should be the same
4. visibiliy cannot be reduced (public to private, protected to default etc)
5. cannot declare new exceptions as long as the new exception is a subclass of originally declared Exception.

A fails because of 4.
C fails because of 5
D fails because of 2.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Answer is B and E for the reasons mentioned by Raghul
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not c?
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any one please,
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii Kenny

c is wrong bec a overriding method must not throw broader checked exception.
but it can throw narrower exception. "Exception" is the broadest exception so it cant be thrown.

regards
anuj
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the overriding method can throw only those exceptions thrown by the overridden method or the subset of exceptions thrown by the overridden method. But in option C, the method is throwing Exception (the generic exception). This is contradicting our rule of exception for overriding. Thats why C is wrong.
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some legal overridden methods without exception. for example look at the following code

if the abstract method amethod() has default access modifier then you will override the following access modifier
public/protected/deault but private is illegal.
if the abstract method amethod() has protected access modifier then you will override the following access modifier
public/protected/ but default & private is illegal.
if the abstract method amethod() has public access modifier then all other access modifiers are illegal.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
can i say that


1)the Stronger access restirction we can have is default for package level (assuming the overridding method is default)

2)(the Stronger access restirction we can have is protected for between two packages (assuming the overridding method is protected in different package)
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to add something in Rahul's answer :
The method Cant not declare new 'Checked' Exceptions untill and unless the exceptions are subclass of the Declared Exception .But the overriden method can declare new 'Runtime Exception'ex. NullPointerException,ArithmaticException etc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic