Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checked exception and inheritance

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys, can't quite understand this one from Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 11408-11412). Enthuware. Kindle Edition.
Given that SomeException is a checked exception, consider the following code:

Which of the following options can be inserted at // 1 without resulting in any compilation or runtime errors? Select 1 option
A.
B.
C.
D.
E. None of these.


Now, the correct answer is C. I don't understand why B is incorrect. The explanation says:
A's m() declares 'throws SomeException', which is a checked exception, while the main() method doesn't. So a.m() must be wrapped in a try/ catch block.
, but, because we're creating a new object of type B even if it has a reference of type A (and B extends A) surely when it calls the m method it will call B's m and not A's (that's inheritance isn't it?) and B's m has no exception declared, so it should be OK, shouldn't it?!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
This one is subtle.

For both choices B and C, it will call the m() method implemented on class B. However, the compiler has issue with one of the options so you don't get that far!

From the compiler's point of view, Option B is calling m() on class A or any subclass of class A. Since class A could throw the checked exception, the compiler takes issue.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,
thanks but I'm still not 100% sure I got it,sorry.
For both choices B and C, it will call the m() method implemented on class B.
Yes this is the way I thought it would be.
But the options are taken independently of one another
Right, sorry I don't get this, I thought we said it calls the method in the subclass B :-) ? And since the method in B, which is the one that gets called, doesn't throw any exception then it should be OK
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Let's try this another way. Do you understand why this method has to throw an exception?


This should make sense as m() as defined in A declares this exception. Now, you agree that there could be an instance of A or B in that reference. If it is B, no worries. The exception won't get thrown. If it is A, no worries. method() declares it might throw an exception.

This is actually what is going on in the original example. Since the B object is stored in an A reference, the compiler treats it as A or any subclass of A. Just like the above method() does.


Better?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler always validates a method call by looking at the declared type of the variable (and not the actual class of the object to which the reference variable may be pointing, because that is known only to the JVM at run time). Here, the declared class of a is A and A's m() declares 'throws SomeException', which is a checked exception. Thus, as far as the compiler is concerned, a call to a.m() can possibly generate a checked exception and the compiler expects that to be either handled using a try/catch or declared in the caller's signature.
Since the  caller, i.e. the main() method, doesn't have this exception declared in its throws clause, the call to a.m() must be wrapped in a try/catch block.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I think I got it guys, thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic