File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Overriding 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 "Regarding Overriding" Watch "Regarding Overriding" New topic
Author

Regarding Overriding

prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
please consider the following code:

import java.io.*;
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub();
try{
myref.test();
}catch(IOException ioe){}
}
void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test() {
System.out.println("In Question05Sub");
}
}
In the above code which version of test() method is called ? I think subclass version is called because of method overriding . But i get a compile error..can anyone please clear my doubt ?
Aiglee Castillo
Greenhorn

Joined: Aug 10, 2006
Posts: 27
Hi

The error you get when you write that code exactly like that is because you have "catch(IOException ioe)" but you are not working with files; if you change that line for "catch(Exception ioe)" then you can compile without problem.

And the test() method called is the one of the subclass because you are writing "Question05Sub myref = new Question05Sub();" and then calling test() with that reference.

EDIT:
You can also change the definition of the method test() like this "void test() throws IOException" so you dont have to change the catch of the main method.
[ August 11, 2006: Message edited by: Aiglee Castillo ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
An instance method in a subclass overrides the method in its super classes that has the same signature and return type. When method overriding occurs, Java virtual machine determines the proper method to call at the program's runtime, not at the compile time. The version of a method is invoked based on the actually object's type at the runtime.
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
Hi,
Thankyou so much for your answer. I was not knowing this before . can you please tell me the reason behind this ? I mean if we dont work with files should we not catch an IOException? But are we free to throw it ? Again when subclass is declared with the throws Exception , its compiling fine though we catch IOException. I will be glad if you can clear me of this doubt.
Thanks in advance
Aiglee Castillo
Greenhorn

Joined: Aug 10, 2006
Posts: 27
Lets see

I mean if we dont work with files should we not catch an IOException? But are we free to throw it ?

You get IOExceptions when an I/O error occurs, and you are free to throw it anytime but I dont see the point to do it :roll:

Again when subclass is declared with the throws Exception , its compiling fine though we catch IOException.

If you try to compile catching an IOException but throwing an Exception, you get a compile error, but you can do it the other way around. You can compile throwing an IOException but catching an Exception because the IOException extends Exception

Maybe someone else can give you more detailed information
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
Hi Aiglee,
My exact question is : Why am i getting a compiler error when i am catching an IOException ? Why dont i get it when i change it to Exception from IOException in the catch block . Even i too dont see the point in catching an IOException there . But this is a question from a mock exam
and i know the hierarchy of Exceptions also ..
Thanks in advance
Aiglee Castillo
Greenhorn

Joined: Aug 10, 2006
Posts: 27
Lets see if I can explain myself

When you try to catch an IOException in the catch statement you get this compile error: "exception java.io.IOException is never thrown in body of corresponding try statement" because, like it says, you are not throwing anywhere in the try block (or the test() method of the subclass) an IOException.

But you dont get that error if you put an Exception in the catch block (instead of the IOException) because it is more general and you can always get an Exception anywhere in you code.

Right?
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
Hi Aiglee,
My exact question is : Why am i getting a compiler error when i am catching an IOException ? Why dont i get it when i change it to Exception from IOException in the catch block . Even i too dont see the point in catching an IOException there . But this is a question from a mock exam
and i know the hierarchy of Exceptions also ..
Thanks in advance
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
Hi,
Sorry for my previous repeated reply. I understood your point. Thankyou so much..
Aiglee Castillo
Greenhorn

Joined: Aug 10, 2006
Posts: 27
You are welcome
Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91

import java.io.*;
public class Question05 {
public static void main(String[] args) {
Question05 myref = new Question05Sub();

//Above Change RV to Question05Sub to Question05

try{
myref.test();
}catch(IOException ioe){}
}
void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test() {
System.out.println("In Question05Sub");
}
}


Now you dont need to remove IOException bcoz compile check for Question05Sub
test method and it already throws IOException and in runtime Question05's test method will be call...


Nilesh Patel
SCJP 1.5 - 87%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Overriding