• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type Object and exception objects

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, this is a weird one, from
Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 13274-13279). Enthuware. Kindle Edition.
Consider the following code:


What would be the output when the above program is compiled and run? (Assume that FileNotFoundException is a subclass of IOException, which in turn is a subclass of Exception) Select 1 option
A. It will print Object Version
B. It will print java.io.IOException Version
C. It will print java.io.FileNotFoundException Version
D. It will not compile.
E. It will throw an exception at runtime.


Ok so, the correct answer here is C. The explanation is that
The reason is quite simple, the most specific method depending upon the argument is called. Here, null can be passed to all the 3 methods but FileNotFoundException class is the subclass of IOException which in turn is the subclass of Object. So, FileNotFoundException class is the most specific class. So, this method is called.

So every class inherit from Object, I didn't know that exception also inherited from Object, I thought they inherited from Throwable?
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but Throwables are also Objects.
 
Narayana Bojja
Ranch Hand
Posts: 130
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason Attin,

Yes. Throwable is inherited from Object class. Check this Java doc Throwable_API.


Hope it helps !
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood, thanks guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic