wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptionhandling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptionhandling" Watch "Exceptionhandling" New topic
Author

Exceptionhandling

Roll
Ranch Hand

Joined: Jan 20, 2000
Posts: 52
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
Yes :-) You and the compiler are both right. Again, do us the favor: WHERE is that question (the answer, really) from?


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Roll
Ranch Hand

Joined: Jan 20, 2000
Posts: 52
I got this Question from Jargon(Exam simulator). http://www.sarga.com/java/jac.htm
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
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


Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
Sonali Salunkhe
Greenhorn

Joined: Feb 02, 2006
Posts: 11
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

Joined: Aug 31, 2004
Posts: 11343

"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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
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.


Raghu<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3
Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
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

Joined: Jan 12, 2006
Posts: 32
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

Joined: Jan 19, 2006
Posts: 584
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.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptionhandling
 
Similar Threads
mock exam Jargon(sarga)
Another mock exam question.
Constructor
call to shadowed static method at runtime!!!
Marcus Green test1 Q16