File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptionhandling

 
Roll
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output when you compile and execute the following program.
Exception is the base class for all the Exception classes.

////////////////////////////////////////////
import java.io.*;

class Base
{
void test() throws IOException {
System.out.println("Base.test()");
String a = null;
//Complex logic goes here
if(a == null)
throw new IOException("");
}

}
////////////////////////////////////////////
public class Child extends Base {

protected void test() throws Exception {
System.out.println("Child.test()");
Base.test(); //Call the parent method
throw new Exception("");
}

static public void main(String[] a) {
try {
new Child().test();
}
catch(Exception e) { }
}
}
Select most appropriate answer.

a) Child.test()
Base.test()
b) Compilation Error: The method void test() declared in class Child cannot override the method of the same signature declared in class Base. Their throws clauses are incompatible.
c) Compilation Error: The method void test() declared in class Child cannot override the method of the same signature declared in class Base.The access modifier is made more restrictive.
d) Runtime error. Cannot make the access modifier more restrictive for test()
<BOLD>
I think the answer should be b) but the given answer was c).
</BOLD>
When i tried to compile the above program i got the compilation error as in b).
Any comments???
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes :-) You and the compiler are both right. Again, do us the favor: WHERE is that question (the answer, really) from?
 
Roll
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this Question from Jargon(Exam simulator). http://www.sarga.com/java/jac.htm
 
Harish Tiruvile
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are 2 reasonxs for getting compile tome error in tis code
1)as mention by u (exception is not handling)
2)The access modifier is made more restrictive.(because overridden method cannot be more private than superclass method)

according to me both answers are right
 
Sonali Salunkhe
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I tried the code here on jdk1.5.
The answer b is correct, overridden method shouldn't throw more exception( super clsss) than the one.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Roll,"

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention!

-Marc
 
Raghuveer Kumarakrishnan
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish,

The access modifier is not made more restrictive. Default access is more restrictive than Protected .The code is fine on that count

Default-- Access Only from same package as parent
Protected --Access Only from same package as parent + Member Inheritance allowed in child no matter what package child is in.

So as discussed above only the Exception overriding is wrong.
 
Raghuveer Kumarakrishnan
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roll,

Welcome to Javaranch.

If the compiler says so I would'nt doubt the compiler(as long as you just copy pasted the code)

It is more useful to figure out why that's happening than doubting the compiler

Just my 2 cents
 
srilatha kareddy
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

class Base is having
void test() throws IOException

class Child is having
protected void test() throws Exception



HERE IS THE RULE : overiding method must have either IOException or its subclass


here Exception is superclass of IOException
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think that even after fix the above test method() in class Child to throw a subclass of Exception the code still doesn't compile just because the call to Base.test() is invalid once test() is not static.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please note that this thread is over 6 years old, so if you're replying to the original post (or some of the older responses), there's a good chance that those users won't see it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic