• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interfaces and Inheritance and exceptions..question

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone explain exceptions and inheritance for this code...All I need to know is how to use methods that throw exceptions in the child class and parent class. I am confused

 
Ranch Hand
Posts: 159
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When thinking about this, you should always think about a case where an exception could 'slip through' the try catch net. It should never be possible.
e.g. If a parent has a method 'a' that throws a FileNotFoundException , then
it can not have a child that overides that method 'a' and that throws an
exception that is broader than its FileNotFoundException (broader than
FileNotFoundException is e.g. IOException because FileNotFoundException
extends IOException). Why not ? Because of polymorphism. One could easily
write Parent p = new Child(); and if somebody would call that method 'a' on
p, then a try{}catch(FileNotFoundException){} would have to be done. And if
the child would be able to throw an IOException (like HttpException), then
the parent's try catch would not suffice. Therefore it can not be allowed.

If the child overides the method 'a' and choses to throw a subclass of the
parents exception or nothing, then there is never a problem since the
FileNotFoundException would always catch that.





[ November 08, 2007: Message edited by: Mark Uppeteer ]
long line edited
[ November 09, 2007: Message edited by: Burkhard Hassel ]
 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That gives me a clear understanding.
 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to using java exceptions...but could you please explain why this code is not working. All the following three files are in the same folder. All I am trying to do is... to do an Example on Inheritance, Interfaces and Exceptions. What do I have to do to make this code function properly. Can I define a method to throw exceptions without actually causing them to happen in the method? This code does not compile though. Need help.

Parent.java - has a method that throws two exceptions


I1.java-has a method defined which throws IOException


MyQuestion.java - has a method with same name but throws nothing.
In this class, I created an instance for this class and want to call the method from the interface.I understand that there is no real use, but I still want to call the interface's method.My intention here is to call the method from the interface and then implement it.
AM I RIGHT OR AM I CONFUSED?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic