• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

inheritance + exceptions

 
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.
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • 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
 
Ranch Hand
Posts: 32
  • 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: 32
  • 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: 32
  • 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.
 
Marshal
Posts: 70327
283
  • 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.
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic