File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces and Inheritance and exceptions..question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces and Inheritance and exceptions..question" Watch "Interfaces and Inheritance and exceptions..question" New topic

Interfaces and Inheritance and exceptions..question

Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
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

Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

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 ]

I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
Thanks. That gives me a clear understanding.
Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
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. - has a method that throws two exceptions a method defined which throws IOException - 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.
I agree. Here's the link:
subject: Interfaces and Inheritance and exceptions..question
jQuery in Action, 3rd edition