Regarding Overriding
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ?
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 ?
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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 ]
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Posts: 48
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
Posts: 27
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Lets see
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:
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
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
Posts: 48
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
Posts: 27
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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
Posts: 48
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
Posts: 48
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
Sorry for my previous repeated reply. I understood your point. Thankyou so much..
Sorry for my previous repeated reply. I understood your point. Thankyou so much..
Aiglee Castillo
Greenhorn
Posts: 27
posted 19 years ago
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...
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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%
| Who knew that furniture could be so violent? Put this tiny ad out there to see what happens: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









