• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OCP exams - Overriding question

 
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I just found question in OCP Practise Exams book:

Exam 2, Q:29:

3. import java.io.*;
4. class Physicist {
5. void think() throws IOException {}
6. }
7. public class Feynman extends Physicist {
8. public static void main(String[] args) {
9. new Feynman().think();
10. }
11. // insert method here
12. }

Which compiles?

A. void think() throws Exception {}
B. void think() throws FileNotFoundException {}
C. public void think() {}
D. protected void think() throws IOException {}
E. private void think() throws IOException {}
F. void think() { int x = 7/0; }


I mark C and F as correct answers. But in book as correct answes is marked B, C, D and F.
How could be B and D correct? They throw an exception , but in main() method is not declared that it could throw exception, so I think that B and D could not be true.
Please somebody explain me what's going on here.
 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B. void think() throws FileNotFoundException {}
FileNotFoundException extends IOException, and hence is a narrower exception. You can throw it. But you cant throw broader exceptions like Exception.


Sorry, for the error. I complied the code and got an error. What you are saying is correct.
The code would compile if think() is not called in main OR if main declares or handles the Exception.


D.protected void think() throws IOException {}
This compiles when i comment think(); in main()
I am not so confident about this one. The overriding method may not have a more restrictive modifier. void think() throws IOException {} is public. Then how is "D" compiling ?

According to me - access modifiers in order of their decreasing restrictiveness :
Private > Default > Protected > Public

Is there something wrong in this approach ?

 
matej spac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion: B, C, D and F are all legal overriding.

But the question under the given class is exactly: "Which of the following methods, inserted independently at line 11, compiles? (Choose all that aply.)"
And the thruth is that only C and F compiles - because B and also D throws an exception, that is not declarad or handled in main().

So is there a bug in book or I do´nt understand something?
 
author
Posts: 9050
21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It could be a mistake in the book, but who in this thread actually ran the code and tried all six options?
 
Ranch Hand
Posts: 34
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I tried to compiled all the options.

First the code:



Now, the errors:
For A:

For B:

For C:

For D:

For E:


For F, it compiled but when I ran it,

 
Francis Zabala
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order for B and D to be correct:



Lines 9 and 12 need to be added. For F, it will run fine as what I think, is an overridden method. Not sure if it is, or is it overloaded...

For C to run, it has to be like this:

 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't followed the details, but in the book, ALL the options include { }, including option C.

Where does that leave us?
 
matej spac
Greenhorn
Posts: 28
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, my mistake, off cource, option C is defined in book: "C. public void think() {}"

but it does not change my opinion, that only C and F are correct :-)
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

matej spac wrote:


but it does not change my opinion, that only C and F are correct :-)

Ikpefua wrote:


Hello all, @Matej I agree with you, (to the best of my knowledge) this code AS IT STANDS is testing your knowledge on two FUNDAMENTAL things;

1. The rules of overriding

2. The rules of Declare or Handle

In this case C and F are correct.

A. Is wrong because the overriding method CANNOT declare broader Exceptions (Even if it declares the adequate exception, it will still NOT compile because the main() method fails to comply with the obligation of Declare or Handle)

B. Is wrong because the main() method fails to comply with the obligation of Declare or Handle

D. Is wrong for reasons already mentioned in B.

E. is wrong because overriding method CANNOT use a more restrictive modifier (Even if it uses the adequate modifier, it will still NOT compile because the main() method fails to comply with the obligation of Declare or Handle)
 
Bert Bates
author
Posts: 9050
21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, there IS an error! Thanks guys for the catch!

main() should throw Exception.

Bert
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic