• Post Reply Bookmark Topic Watch Topic
  • New Topic

About explicitly defining a subclass constructor  RSS feed

 
vijayk kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Members,

I heard if a super class constructor(default or argumented) throws an exception it is MUST that the subclass constructor also throw exception.

Will it holds for unchecked exceptions?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijayk kumar:
Dear Members,

I heard if a super class constructor(default or argumented) throws an exception it is MUST that the subclass constructor also throw exception.

Will it holds for unchecked exceptions?


Read it here...

Exceptions

This program will compile...




Naseem
[ July 29, 2006: Message edited by: Naseem Khan ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijayk kumar:

Will it holds for unchecked exceptions?


No, but only because unchecked exceptions, by definition, never need to be declared.
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:



Naseem

[ July 29, 2006: Message edited by: Naseem Khan ]



Yes, this code WILL compile. The reason for that is because Sub is NOT DECLARED TO EXTENDS Super! Check your code next time before you post.

In fact, if Sub did extend Super, Sub would NOT compile, because the constructor Super() is implicitly invoked by the constructor Sub(). Hence, it is possible that Super() will throw a checked exception, and the constructor Sub() must either catch or throw that checked exception. My compiler tells me there is an "Unhandled Exception type IOException" in Sub().

- Adam
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!