This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance + exceptions  RSS feed

 
Vidur Koushik
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been going through exceptions chapter and I find it difficult to solve problems when inheritance is involved.<br />
Can someone post 5 questions that combine these topics,so that I can learn more when inheritance is involved.<br />
Thank You.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why dont you practice on sites like "HackerRank" just  google it you will find a link to this site their are plenty of programs you can practice their
 
Vidur Koushik
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean to say SCJP questions.5 questions covering important aspects of inheritance+exceptions
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link has plenty of examples you can practice those to understand better,here it is-->
Thinking in java-Error handling via exceptions
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remember these three points. It would help you to solve many of the questions in this topic. After reading these poins, try out some sample problems available in the web.

1. A subclass method if overriding a superclass method should throw the same exception or a subclass of the exception or should not throw any exception.
2. A subclass method if overriding a superclass method should throw only the same exception and not its subclass when the superclass method is accessed by the subclass using super.methodname().
3. If a superclass method does not declare an exception, then the subclass overridden method cannot declare any checked exception but it can declare an unchecked exception.
 
Vidur Koushik
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautham Muralidharan wrote:Just remember these three points. It would help you to solve many of the questions in this topic. After reading these poins, try out some sample problems available in the web.

1. A subclass method if overriding a superclass method should throw the same exception or a subclass of the exception or should not throw any exception.


Can it also throw run time exceptions or it's child classes??
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as run time exceptions are unchecked, you can always declare run time exception to any method.
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will sum up the exception in inheritance concept with one single program.





In the super class 'TestSuperClass', in line 7, the method tester declares a FileNotFoundException.
So, the overridden method tester in the subclass should either throw the FileNotFoundException, or its superclass IOException, or any runtime exception like ArrayIndexOutOfBoundsException, or not declare any exception at all. So in the above program, replacing line 15 with either line 16 or 17 or 18 will not throw any error.

In case the line 7 in the above program is changed to , then the line 15 can either be replaced with line 17 or 18, not line 16. This is because, FileNotFoundException is a subclass of IOException.

Please note that the main method in line 23 declares a FileNotFoundException. If I replace line 23 by line 25, then line 30 will throw an error. This is because, any method that calls a method declaring an exception, also should declare the same exception or its parent class. Hence line 23 when replaced by 24 works fine.

Any method may or may no declare a runtime exception. Java interpreter will not throw any error for either declaring or not declaring a run time error. Just randomly add 'throws ArrayIndexOutOfBoundsException' or any other run time to any method in any program you execute. Compiler won't bother about the run time error.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidur Koushik wrote:. . . Can it also throw run time exceptions or it's child classes??
Do you mean RuntimeException? It can throw such an exception, but it must not. If you decide to throw such an Exception from an overriding method, you are breaching the general contract of the superclass method. I know Bruce Eckel will tell you to take a checked Exception and wrap it in an unchecked Exception, but that is simply a way to evade the compile‑time checking.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!